日期:2014-05-18  浏览次数:20526 次

字符串拼接
SQL code
--需求:从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)


------解决方案--------------------
SQL code

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

------解决方案--------------------
SQL code
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)

------解决方案--------------------
SQL code

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

------解决方案--------------------
探讨

SQL code

--这句帮我改一下
select @sql='select ''if exists(select 1 from sysobjects where id=object_id(''+a.name+'')) and xtype='+@xtype+' begin drop function dbo.''+a.name+'' end'' as b'
select @sql=@sql+……

------解决方案--------------------
探讨

引用:


magician547,我想自动生成删除函数的语句,有没有其他的方法?

------解决方案--------------------
探讨

引用:


magician547,我想自动生成删除函数的语句,有没有其他的方法?