数据库常用操作语句
2009-07-28 20:50
369 查看
前些时候,经常的对数据库进行的反复的操作,保留了些经常用的操作语句,总结一下跟大家分享一下:
一、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
一、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
相关文章推荐
- 数据库基本操作SQL(创建表 添加主键 添加列的常用语句)
- mysql数据库常用的基本SQL语句--数据库数据操作
- 新 好用的 YII数据库操作常用语句
- 数据库中常用的操作语句
- 常用sql语句-基础(数据库操作,表操作)
- 常用的SQL语句对数据库进行操作
- 数据库常用sql语句和操作
- 自己常用的数据库操作语句
- Oracle 数据库常用操作语句大全
- oracle数据库常用操作语句
- Hibernate学习笔记三:常用数据库操作语句
- MySQL存储过程详解 mysql 存储过程mysql存储过程详解 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored
- Oracle 数据库常用操作语句
- 数据库操作常用SQL语句
- SQLite轻量级数据库,操作数据常用语句
- MySql常用操作语句(1:启动、连接数据库及用户操作)
- T-SQL常用数据库操作语句
- 数据库常用操作的SQL语句
- 常用数据库操作语句(2)
- Oracle 常用数据库sql语句操作