【实用SQL脚本】生成数据发布的INSERT语句
2012-05-19 08:03:00 来源:WEB开发网【问题】DB结构的发布,SQL Server的管理器可以生成脚本;那么数据如何发布呢?
【思路】生成INSERT INTO ... VALUES...语句
/*-- =============================================
-- Author: Yew
-- Create date: 2012-05-18
-- Description: 生成数据发布的INSERT脚本
----TestCode---------------------------
EXEC [TOOL].[Script_Insert]
@Schema = 'COMMON'
,@Table = 'Config'
,@Filter = '[Key] LIKE ''test%'' '
,@Field0 = '[Key]'
,@Field1 = 'SubKey'
,@Field2 = 'Value'
,@Field3 = null
,@Field4 = null
EXEC [TOOL].[Script_Insert]
@Schema = 'COMMON'
,@Table = 'MasterData'
,@Filter = 'Category = ''SheetItem_PreloadType'' '
,@Field0 = 'Category'
,@Field1 = 'Code'
,@Field2 = 'Name'
,@Field3 = 'Ord'
,@Field4 = null
----History---------------------------
-- =============================================*/
ALTER PROCEDURE [TOOL].[Script_Insert]
@Schema sysname = 'dbo'
,@Table sysname
,@Filter sysname = null
,@Field0 sysname
,@Field1 sysname = null
,@Field2 sysname = null
,@Field3 sysname = null
,@Field4 sysname = null
AS
BEGIN
SET NOCOUNT ON;
-- 0. Define Const
DECLARE @NL varchar(2) --NewLine
SET @NL = char(13) + char(10)
DECLARE @vSql NVARCHAR(max)
,@objName sysname
,@fieldList sysname
-- 1. prepare the statement
IF @Schema = ''
SET @Schema = 'dbo'
SET @objName = @Schema + '.' + @Table
SET @vSql = 'SET QUOTED_IDENTIFIER OFF;
SELECT
"INSERT INTO ' + @objName + '(' + @Field0
IF @Field1 IS NOT NULL
SET @vSql = @vSql + ' ,' + @Field1
IF @Field2 IS NOT NULL
SET @vSql = @vSql + ' ,' + @Field2
IF @Field3 IS NOT NULL
SET @vSql = @vSql + ' ,' + @Field3
IF @Field4 IS NOT NULL
SET @vSql = @vSql + ' ,' + @Field4
SET @vSql = @vSql + ' )'
+ @NL + 'VALUES(" + QuoteName(' + @Field0 + ', Char(39))'
IF @Field1 IS NOT NULL
SET @vSql = @vSql + @NL + ' +", " + QuoteName(IsNull(' + @Field1 + ', ""), Char(39))'
IF @Field2 IS NOT NULL
SET @vSql = @vSql + @NL + ' +", " + QuoteName(IsNull(' + @Field2 + ', ""), Char(39))'
IF @Field3 IS NOT NULL
SET @vSql = @vSql + @NL + ' +", " + QuoteName(IsNull(' + @Field3 + ', ""), Char(39))'
IF @Field4 IS NOT NULL
SET @vSql = @vSql + @NL + ' +", " + QuoteName(IsNull(' + @Field4 + ', ""), Char(39))'
SET @vSql = @vSql + @NL + ' +")"'
+ @NL + 'FROM ' + @objName
IF @Filter IS NOT NULL
SET @vSql = @vSql + @NL + 'WHERE ' + @Filter
-- 2.run it
PRINT @vSql
EXEC(@vSql)
END
- ››SQL Server 2008 R2 下如何清理数据库日志文件
- ››sqlite 存取中文的解决方法
- ››SQL2005、2008、2000 清空删除日志
- ››实用的Win 7远程桌面连接技巧
- ››SQL Server 2005和SQL Server 2000数据的相互导入...
- ››sql server 2008 在安装了活动目录以后无法启动服...
- ››实用的减短Windows 7开机时间的办法
- ››sqlserver 每30分自动生成一次
- ››sqlite 数据库 对 BOOL型 数据的插入处理正确用法...
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
更多精彩
赞助商链接