字符串拼接
--需求:从sysobjects表取出xtype=@xtype的数据,并自动生成类似于 --if exists(select 1 from sysobjects where id=object_id('DFC_QtyAdd')) and xtype='FN' begin drop function dbo.DFC_QtyAdd end 的语句declare @sql varchar(1000),@xtype char(2)select @xtype='FN'select @sql='请帮忙拼接字符串'exec(@sql)
DECLARE @Sql VARCHAR(1000)DECLARE @xtype VARCHAR(10) = 'FN'SET @Sql = 'if exists(select 1 from sysobjects where id=object_id(''DFC_QtyAdd'') and xtype=''' + @xtype + ''' )begin drop function dbo.DFC_QtyAdd end'PRINT @Sql
[解决办法]
declare @sql varchar(1000),@xtype char(2)select @xtype='FN'select @sql='if exists(select 1 from sysobjects where id=object_id(''DFC_QtyAdd'') and xtype='''+@xtype+''') begin drop function dbo.DFC_QtyAdd end'exec(@sql)
[解决办法]
DECLARE @Sql VARCHAR(1000)DECLARE @xtype VARCHAR(10) = 'FN'DECLARE @name VARCHAR(100) = 'DFC_QtyAdd'SET @Sql = 'if exists(select 1 from sysobjects where id=object_id(''' + @name + ''') and xtype=''' + @xtype + ''' )begin drop function dbo.' + @name + 'end'PRINT @Sql
[解决办法]