--@brother = int create procdure my_proce @表名 varchar(20) , @brother int as begin exec('update ' + @表名 + ' set brother = ' + cast(@brother) + ' where brother is null') end go--@brother = varchar create procdure my_proce @表名 varchar(20) , @brother varchar(20) as begin exec('update ' + @表名 + ' set brother = ''' + cast(@brother) + ''' where brother is null') end go
CREATE PROCEDURE p_Test @TableName sysname , @id int , @brother varchar(100) AS DECLARE @Cmd NVARCHAR(500) DECLARE @Res INTSET @Cmd = N'SELECT @Res = CASE WHEN EXISTS( Select 1 from ' + @TableName + ' WHERE ID = ' + CAST( @id as nvarchar ) + ' AND Brother IS NOT NULL AND Brother <> ' + Cast( @brother as nvarchar ) + ' ) THEN 1 ELSE 0 END 'EXEC sp_executesql @Cmd, N'@Res int output', @ResIF ( @Res = 1 ) BEGIN SET @Cmd = 'UPDATE ' + @TableName + ' SET brother = ' + Cast( @brother as nvarchar ) + ' WHERE id = ' + CAST( @id as nvarchar ) EXEC sp_executesql @Cmd END
create procdure my_proce @表名 varchar(20) , @brother int
as
begin
exec('update ' + @表名 + ' set brother = ' + cast(@brother) + ' where brother is null')
end
go--@brother = varchar
create procdure my_proce @表名 varchar(20) , @brother varchar(20)
as
begin
exec('update ' + @表名 + ' set brother = ''' + cast(@brother) + ''' where brother is null')
end
go
, @id int
, @brother varchar(100)
AS
DECLARE @Cmd NVARCHAR(500)
DECLARE @Res INTSET @Cmd = N'SELECT @Res = CASE WHEN EXISTS( Select 1 from '
+ @TableName
+ ' WHERE ID = ' + CAST( @id as nvarchar )
+ ' AND Brother IS NOT NULL AND Brother <> '
+ Cast( @brother as nvarchar )
+ ' ) THEN 1 ELSE 0 END 'EXEC sp_executesql @Cmd, N'@Res int output', @ResIF ( @Res = 1 )
BEGIN
SET @Cmd = 'UPDATE ' + @TableName + ' SET brother = '
+ Cast( @brother as nvarchar ) + ' WHERE id = '
+ CAST( @id as nvarchar )
EXEC sp_executesql @Cmd
END