将表数据生成Insert脚本 比较好用的生成插入语句的SQL脚本
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: 华岭
-- Create date: 2008-10-28
-- Description: 将
复制代码 代码如下: set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- ============================================= -- Author: 华岭 -- Create date: 2008-10-28 -- Description: 将表数据生成Insert脚本 -- Demo : exec pCreateInsertScript 'BexmCodeType','dictypeid = 61' -- ============================================= alter proc [dbo].pCreateInsertScript (@tablename varchar(256),@con nvarchar(400)) as begin set nocount on declare @sqlstr varchar(4000) declare @sqlstr1 varchar(4000) declare @sqlstr2 varchar(4000) select @sqlstr='select ''insert '+@tablename select @sqlstr1='' select @sqlstr2='(' select @sqlstr1='values (''+' select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end' when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end' when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end' when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' when a.xtype =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end' when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end' when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' else '''NULL''' end as col,a.colid,a.name from syscolumns a where a.id = object_id(@tablename) and a.xtype 189 and a.xtype 34 and a.xtype 35 and a.xtype 36 )t order by colid select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename + ' where 1=1 and ' + isnull(@con,'') print @sqlstr exec( @sqlstr) set nocount off end 【将表数据生成Insert脚本 比较好用的生成插入语句的SQL脚本】相关文章: ★ SQLserver 2008将数据导出到Sql脚本文件的方法 ★ 在sp_executesql中使用like字句的方法 ★ JDBC连接Sql Server 2005总结 ★ SQL SERVER清除日志的方法 ★ sql 触发器使用例子 ★ mssql中得到当天数据的语句 ★ SQLite数据库 ★ 一道sql面试题附答案 ★ 收缩后对数据库的使用有影响吗? ★ SQL Server数据库管理常用的SQL和T-SQL语句 在我们的项目中经常需要用到分面功能mssql复制表,而我以前呢用的方法现在看起来都是那麽的笨拙,当时是这样做的,每当要进行数据分页时就专们针对那个表做分页,大家别笑,以前确实好笨。呵呵,虽然当时也有一个想法就是希望能够传入一张表进去进行操作,但那样的话编译是通不过的,因为FROM后面操作的是表变量,而不能是我们自定义的变量,所以当时没有深追究,现在为当时不深入学习而BS一下。 动态SQL需要准备以下内容: 1、@SQL 拼接后的SQL语句,可以是你任意需要的SQL语句如:SET @SQL='SELECT * FROM table WHERE ID=@id' 注意此处的@SQL必须且只能是NTEXT、NVARCHAR、NCHAR类型,如果是其它类型的话其它地方明明没有问题却会报 "过程需要类型为 'ntext/nchar/nvarchar' 的参数"这个错误。同时,如果这里需要传入表名称的话则应这样写:SET @SQL='SELECT * FROM '+@table+'WHERE ID=@id' ,因为上面传入的值是文本类型故会报错。 2、@parameters 所拼接的SQL语句里面的参数,按上面的话这里应该是:SET @parameters='@id INT' 同时这个参数的类型也必须且只能是NTEXT、NVARCHAR、NCHAR类型 3、调用:sp_executesql param1(,param2) 其中param1一般我们作为是@SQL,后面的参数则是我们在@sql中的参数了,但这里要注意的是传参的时候必须是对应的: 复制代码 代码如下: DECLARE @InputId INT ; SET @InputId=1; param2为:@id=@InputId; 以下是写的一个简单的通用分页,有需要可以自行修改: 复制代码 代码如下: ALTER PROCEDURE sp_pager ( @TableName nvarchar(50), -- 表名 @ReturnFields nvarchar(200) = '*', -- 需要返回的列 @PageSize int = 50, -- 每页记录数 @PageIndex int = 1 -- 当前页码 ) AS DECLARE @SQL NVARCHAR(1000) DECLARE @paramters NVARCHAR(200) BEGIN SET NOCOUNT ON SET @SQL='SELECT '+@ReturnFields+' FROM '+@TableName+' WHERE ID>(SELECT TOP 1 ID FROM (SELECT TOP '+CAST(@PageSize*@PageIndex AS VARCHAR)+' ID FROM '+@TableName+ ' ORDER BY ID )AS A ORDER BY ID DESC)' PRINT @SQL EXECUTE sp_executesql @SQL,@paramters,@columns=@ReturnFields END 【动态SQL语句使用心得】相关文章: ★ SQL查询语句精华使用简要第1/2页 ★ SQL 查询分析中使用net命令问题 ★ MSSQL经典语句 ★ SQL语句示例 ★ SQL 经典语句 ★ 有用的SQL语句(删除重复记录,收缩日志) ★ SQL2005行触发器 ★ SQL 连接查询语法及使用 ★ SQL2005查询表结构的SQL语句使用分享 ★ 精妙的SQL语句第1/2页 创建存储过程 表名和比较字段可以做参数的存储过程 Create PROCEDURE sp_getTeacherInfo @TblName nvarchar(30), -- 表名 @CmpStr nvarchar(30), -- 参与比较的值 @TblAtr nvarchar(30) -- 参与比较的字段 AS DECLARE @sql varchar(4000) SET @sql = 'select * from ' + @TblName + ' where ' + @TblAtr + '=' + @CmpStr EXEC (@sql) 表 tbl_TeacherInfo Exec sp_getTeacherInfo 'tbl_TeacherInfo','TeaNo', '07417502' // 注意:像这样的调用是错误的 还原成查询语句 select * from tbl_TeacherInfo where TeaNo = 07417502 之所以没报错,是因为参数'07417502'被误认为了 整型,进行了整数的比较 Exec sp_getTeacherInfo 'tbl_TeacherInfo','Name','楚留香' // 报错 还原成查询语句 select * from tbl_TeacherInfo where TeaNo = 楚留香 // 显然是错误的 正确的调用方法 Exec sp_getTeacherInfo 'tbl_TeacherInfo','Name',"'楚留香' " 还原成查询语句 select * from tbl_TeacherInfo where TeaNo = '楚留香' 常规存储过程的创建 Create PROCEDURE sp_AddRowToLogin @TeaNo nvarchar(100), -- 比较字段 @TeaName nvarchar(100) -- 比较字段 AS insert into tbl_UserLogin values(@TeaNo,@TeaName,@TeaNo,0) 除了指定列其他列都返回的存储过程 CREATE PROCEDURE sp_Alter @TblName nvarchar(30) -- 表名 AS declare @sql varchar(1000) select @sql='select ' select @sql=@sql+name+',' from syscolumns where id=object_id(@TblName) and name not in ('ID','TeaNo') select @sql=left(@sql,len(@sql)-1) select @sql=@sql+' from ' + @TblName exec (@sql) // 除了ID和TeaNo两列不返回,其他都返回 【SQL创建的几种存储过程】相关文章: ★ SQL Server 2005删除日志文件的几种方法小结 ★ SQL Server下几个危险的扩展存储过程 ★ AspNetPager分页控件 存储过程 ★ 一个分页存储过程代码 ★ SQL Server的复制功能 ★ SQL SEVER创建登录帐号 ★ 目前用到的两个分页存储过程代码 ★ SQL Server 存储过程解析 ★ 一个比较实用的大数据量分页存储过程 ★ SQL SERVER存储过程的使用 (编辑:海南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |