数据库常用操作语句

前些时候,经常的对数据库进行的反复的操作,保留了些经常用的操作语句,总结一下跟大家分享一下:
一、SQl2005兼容SQL2000语句
     exec sp_dbcmptlevel 'dsc040320_db','80'
二、生成清库语句
     select 'delete from ' + name from sysobjects where xtype='u' order by name
三、SQL2005 安装时 “性能监视器计数器要求(错误)”
   请下载下面的文档学习(传了好几遍传不上去,改天补上)
四、用存储过程生成插入语句

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

create   proc [dbo].[spGenInsertSQL1] (@tablename varchar(256))

as

begin

declare @sql varchar(8000)

declare @sqlValues varchar(8000)

set @sql =' ('

set @sqlValues = 'values (''+'

select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],'

  from

      (select case

                when xtype in (48,52,56,59,60,62,104,106,108,122,127)                              

                     then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'

                when xtype in (58,61)

                     then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'

               when xtype in (167)

                     then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'

                when xtype in (231)

                     then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'

                when xtype in (175)

                     then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar)  + '))+'''''''''+' end'

                when xtype in (239)

                     then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar)  + '))+'''''''''+' end'

                else '''NULL'''

              end as Cols,name

         from syscolumns

        where id = object_id(@tablename)

      ) T

set @sql ='select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename

--print @sql

exec (@sql)

end


 


原文地址:https://www.cnblogs.com/ITRoad/p/1533330.html