简体   繁体   中英

SQL dynamic columns and Update multiple columns

I have a table UserPermission which has a number of columns of TINYINT type. eg Read, Write, Update, Delete, Access etc.

I get three parameters in the stored procedure: @UserId, @ColNames, @ColValues where @ColNames and @ColValues are comma separated values.

How can I insert or update the table row (if already exists) with the passed column names and corresponding values.

I try to write the dynamic query which runs fine for INSERT but I was unable to write the UPDATE query dynamically with each column and its value to be concatenate.

Any response would be appreciated

Thanks in advance.

This is a somewhat dirty way to do what you require. However, if you create the following Stored Procedure:

CREATE FUNCTION [dbo].[stringSplit]
(
    @String NVARCHAR(4000),
    @Delimiter NCHAR(1)
)
RETURNS TABLE
AS
RETURN
(
    WITH Split(stpos,endpos)
    AS(
        SELECT 0 AS stpos, CHARINDEX(@Delimiter,@String) AS endpos
        UNION ALL
        SELECT endpos+1, CHARINDEX(@Delimiter,@String,endpos+1)
            FROM Split
            WHERE endpos > 0
    )
    SELECT 'Id' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
        'Data' = SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)
    FROM Split
)

You can then use that Procedure to join the data together:

DECLARE @TotalCols INT
DECLARE @TotalVals INT

SET @TotalCols = (
        SELECT COUNT(ID) AS Total
        FROM dbo.stringSplit('department, teamlead', ',')
        );
 SET @TotalVals = (
         SELECT COUNT(ID) AS Total
         FROM dbo.stringSplit('IT, Bob', ',')
         );

 IF @TotalCols = @TotalVals
 BEGIN
     IF OBJECT_ID('tempdb..#temptable') IS NOT NULL
         DROP TABLE #temptable

     CREATE TABLE #temptable (
        ColName VARCHAR(MAX) NULL
        ,ColValue VARCHAR(MAX) NULL
        )

     INSERT INTO #temptable
     SELECT a.DATA
         ,b.DATA
    FROM dbo.stringSplit('department, teamlead', ',') AS a
    INNER JOIN dbo.stringSplit('IT, Bob', ',') AS b ON a.Id = b.Id

    SELECT *
    FROM #temptable;
END

It's not very efficient, but it will bring you the desired results.

You can then use the temp table to update, insert and delete as required.

Instead of having a comma delimited list I would create a separate parameter for each Column and make its default value to NULL and in the code update nothing if its null or insert 0. Something like this....

CREATE PROCEDURE usp_UserPermissions
 @UserID INT 
,@Update INT = NULL  --<-- Make default values NULL
,@Delete INT = NULL
,@Read   INT = NULL
,@Write  INT = NULL
,@Access INT = NULL
AS
BEGIN
  SET NOCOUNT ON;

Declare @t TABLE (UserID INT, [Update] INT,[Read] INT
                 ,[Write] INT,[Delete] INT,[Access] INT)

INSERT INTO @t (Userid, [Update],[Read],[Write],[Delete],[Access])
VALUES (@UserID , @Update , @Read, @Write , @Delete, @Access)   


IF EXISTS (SELECT 1 FROM UserPermission WHERE UserID = @UserID)
  BEGIN 
       UPDATE up        -- Only update if a value was provided else update to itself
        SET up.[Read]   = ISNULL(t.[Read]   , up.[Read])
           ,up.[Write]  = ISNULL(t.[Write]  , up.[Write])
           ,up.[Update] = ISNULL(t.[Update] , up.[Update])
           ,up.[Delete] = ISNULL(t.[Delete] , up.[Delete])
           ,up.[Access] = ISNULL(t.[Access] , up.[Access])
       FROM UserPermission up 
       INNER JOIN @t t ON up.UserID = t.UserID
  END
ELSE 
  BEGIN
         -- if already no row exists for that User add a row 
         -- If no value was passed for a column add 0 as default
    INSERT INTO UserPermission (Userid, [Update],[Read],[Write],[Delete],[Access])
    SELECT Userid
         , ISNULL([Update], 0)
         , ISNULL([Read], 0)
         , ISNULL([Write], 0)
         , ISNULL([Delete], 0)
         , ISNULL([Access], 0)
    FROM @t
  END

END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM