我创建了一个触发器,可以在更新后将对象(笔记本电脑,PC,打印机......)的状态更改为预订。
脚本在动态sql中(环境是SQL Server 2008 R2):
DECLARE @CMDB_ID int, @variable varchar(128), @CI_TYPE_ID int, @STATUS varchar(128),
@mrSTATUS varchar(128), @stock_status varchar(128), @SERIAL_NUMBER varchar(128)
SET @CMDB_ID = 2
SET @stock_status = 'En stock'
SET @mrSTATUS = 'APPROUVEE'
SET @STATUS ='RESERVE'
Declare @sql nvarchar(max), @CRLF varchar(2)
SET @CRLF = CHAR(13) + CHAR(10);
SET @SQL = N'Create trigger [dbo].[CHANGEMENT_ETAT_CI_EN RESERVATION]' + @CRLF
SET @SQL = @SQL + N'ON [dbo].[MASTER4]'+ @CRLF
SET @SQL = @SQL + N'AFTER UPDATE'+ @CRLF
SET @SQL = @SQL + N'AS' + @CRLF
SET @SQL = @SQL + N'if UPDATE(RECHERCHE__bNOUVEAU__bSERIAL__bNUMBER)' + @CRLF
SET @SQL = @SQL + N'BEGIN' + @CRLF
SET @SQL = @SQL + N'SET NOCOUNT ON;' + @CRLF
SET @SQL = @SQL + N' ' + @CRLF
SET @SQL = @SQL + N' DECLARE @CMDB_ID int,@variable varchar(128), @CI_TYPE_ID int, @STATUS varchar(128),@mrSTATUS varchar(128), @stock_status varchar(128), @SERIAL_NUMBER varchar(128)' + @CRLF
SET @SQL = @SQL + N'SET @CMDB_ID =' + CAST(@CMDB_ID as varchar(8))+ @CRLF
SET @SQL = @SQL + N'SET @status = ''' + CAST(@STATUS as varchar(128))+ '''' + @CRLF
SET @SQL = @SQL + N'SET @STOCK_STATUS = ''' + CAST(@STOCK_STATUS as varchar(128))+ '''' + @CRLF
SET @SQL = @SQL + N'SET @mrstatus = ''' + CAST(@mrstatus as varchar(128))+ '''' + @CRLF
SET @sql = @sql + N' ' + @CRLF
SET @SQL = @SQL + N'DECLARE c1 cursor for' + @CRLF
SET @SQL = @SQL + N'SELECT RECHERCHE__bNOUVEAU__bSERIAL__bNUMBER'+ @CRLF
SET @SQL = @SQL + N'FROM inserted'+ @CRLF
SET @SQL = @SQL + N'WHERE mrSTATUS = @mrSTATUS'+ @CRLF
SET @SQL = @SQL + N'open c1'+ @CRLF
SET @SQL = @SQL + N'FETCH NEXT FROM c1 INTO @SERIAL_NUMBER'+ @CRLF
SET @SQL = @SQL + N'SET @variable =' + '(SELECT Num__UGLJ_ro__bde__bs__UGLJ_rie FROM [dbo].[CI&IN&STOCK] WHERE Num__UGLJ_ro__bde__bs__UGLJ_rie= @SERIAL_NUMBER)'+ @CRLF
SET @SQL = @SQL + N'SET @CI_TYPE_ID =' + '(SELECT [dbo].[CI&IN&STOCK].CI_TYPE_ID FROM [dbo].[CI&IN&STOCK] WHERE Num__UGLJ_ro__bde__bs__UGLJ_rie= @SERIAL_NUMBER)'+ @CRLF
SET @SQL = @SQL + N'WHILE @@FETCH_STATUS = 0'+ @CRLF
SET @SQL = @SQL + N'BEGIN'+ @CRLF
SET @SQL = @SQL + N'Declare @sql nvarchar(max), @CRLF varchar(2)'+ @CRLF
SET @SQL = @SQL + N'SET @sql=''''' + @CRLF
SET @SQL = @SQL + N'SET @CRLF = CHAR(13) + CHAR(10)' + @CRLF
SET @SQL = @SQL + N'SET @sql= @sql + N''update CMDB'' + CAST(@CMDB_ID as varchar(8)) + ''_CI_'' + CAST(@CI_TYPE_ID as varchar(8)) + @CRLF' + @CRLF
SET @SQL = @SQL + N'SET @sql= @sql + N''SET STATUS = '''''' + @STATUS + '''''''' + @CRLF' + @CRLF
SET @SQL = @SQL + N'SET @sql= @sql + N''WHERE Num__UGLJ_ro__bde__bs__UGLJ_rie ='' + CAST(@variable as varchar(128)) + '' and STATUS = '''''' + @stock_status + '''''''' + @CRLF' +@CRLF
SET @SQL = @SQL + N'EXEC sp_executesql @sql'+ @CRLF
SET @SQL = @SQL + N'fetch next from c1 into @SERIAL_NUMBER' + @CRLF
SET @SQL = @SQL + N'END'+ @CRLF
SET @SQL = @SQL + N'close c1'+ @CRLF
SET @SQL = @SQL + N'DEALLOCATE c1'+ @CRLF
SET @SQL = @SQL + N'END'+ @CRLF
EXEC sp_executesql @sql
当我触发此脚本时抛出MASTER4表的更新时,会出现以下错误:
消息207,级别16,状态1,行1 列名称无效:' TRF3110WJS'
即使价值' TRF3110WJS'不是列名,而是列值。
有任何支持的想法吗?
注意:如果我的表名是静态的而不是动态的,那么脚本效果很好。
答案 0 :(得分:0)
问题出在Dynamic SQL中,它创建了更多动态的sql
SET @SQL = @SQL + N'SET @sql= @sql + N''WHERE Num__UGLJ_ro__bde__bs__UGLJ_rie ='' + CAST(@variable as varchar(128)) + '' and STATUS = '''''' + @stock_status + '''''''' + @CRLF' +@CRLF
周围需要更多报价
CAST(@variable as varchar(128))
位。
我认为应该这样做
SET @SQL = @SQL + N'SET @sql= @sql + N''WHERE Num__UGLJ_ro__bde__bs__UGLJ_rie ='''''' + CAST(@variable as varchar(128)) + '''''' and STATUS = '''''' + @stock_status + '''''''' + @CRLF' +@CRLF