我有一个表UserPermission
,其中包含多个TINYINT
类型的列。例如,读,写,更新,删除,访问等。
我在存储过程中得到三个参数:@UserId, @ColNames, @ColValues
其中@ColNames
和@ColValues
是逗号分隔值。
如何使用传递的列名和相应的值插入或更新表行(如果已存在)。
我尝试编写适用于INSERT
的动态查询,但我无法动态编写UPDATE
查询,每列都要连接它的值。
任何回复都将不胜感激
提前致谢。
答案 0 :(得分:0)
这是一种有点脏的方式来做你需要的。但是,如果您创建以下存储过程:
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
)
然后,您可以使用该过程将数据连接在一起:
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
效率不高,但会带来理想的效果。
然后,您可以根据需要使用临时表进行更新,插入和删除。
答案 1 :(得分:0)
我没有逗号分隔列表,而是为每个列创建一个单独的参数,并将其默认值设置为NULL,如果为null或插入0,则代码更新中没有任何内容。像这样....
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