我在数据库中有一个主表。
示例菜单表
+-----------+-----------+-------------+---------+------------------------+
| Id | Key | Display Text| ParentId| CreatedOn
+-----------+-----------+-------------+---------+------------------------+
| 1 | Home | Home | NULL |2014-01-14 21:17:37.387 |
| 2 | About | About Us | NULL |2014-01-14 21:17:37.387 |
| 3 | Contact | Contact Us | NULL |2014-01-14 21:17:37.387 |
+-----------+-----------+------+------+---------+------------------------+
我曾经为每条记录生成如下所示的主数据脚本。
IF NOT EXISTS(SELECT 1 FROM [Menu] WHERE Id=1 AND Key='Home')
BEGIN
SET IDENTITY_INSERT [dbo].[Menu] ON
INSERT INTO [dbo].[Menu]
(Id
,[Key]
,[DisplayText]
,[ParentId]
,[CreatedOn])
VALUES
(1
,'Home'
,'Home'
,NULL
,GETDATE()
)
SET IDENTITY_INSERT [dbo].[Menu] OFF
END
GO
- 为所有70条记录重复相同的手动记录创建工作。其他主数据(10k rows
)
但是,另一个数据库中的某些现有表ApplicationMenu
具有相同的列,数据类型。我们想通过一些存储过程自动为我们生成以下脚本。
是否可以创建如下的程序
CREATE PROCEDURE spGenerateInsertScripts
(
@SourceTableName VARCHAR(100),
@ExistsWhereClauseTemplate NVARCHAR(1000),
@TargetTableName VARCHAR(100)
)
BEGIN
-- In some loop create those above insert statements
END
我们想执行如下
exec spGenerateInsertScripts 'ApplicationMenu'
, 'WHERE Id={Id} AND Key={Key}'
, 'Menu'
{Id}&将从现有表格的每一行读取{Key} 并替换。
这实际上会减少我们的手工工作量。
注意: 的
我们无法使用SQL server insert脚本生成工具,因为我们要检查数据是否存在以及需要保留用户使用我们的应用程序添加的记录。
需要生成插入脚本,以便我们可以在将来运行,即使ApplicationTable不可用
是否可以编写这样的过程来根据存在从其他表生成插入脚本?就像sql server Generate Scripts work for table creation by looking into INFORMATION_SCHEMA
表一样,我期待的方式也是如此。
程序的最终输出将类似于PRINT @insert_Sql_Statements
答案 0 :(得分:3)
您的数据
DECLARE @Table TABLE(Id INT, [Key] VARCHAR(30),[Display Text] VARCHAR(30), ParentId INT, CreatedOn DATETIME)
INSERT INTO @Table VALUES
(1,'Home' ,'Home' ,NULL, '2014-01-14 21:17:37.387'),
(2,'About' ,'About Us' ,NULL, '2014-01-14 21:17:37.387'),
(3,'Contact','Contact Us',NULL, '2014-01-14 21:17:37.387')
查询创建脚本
SELECT N'IF NOT EXISTS(SELECT 1 FROM [Menu] WHERE Id='+ CAST(Id AS NVARCHAR(10))
+ ' AND Key='''+ CAST([Key] AS NVARCHAR(1000)) +''')' + CHAR(10)
+ N'BEGIN ' + CHAR(10) + '
SET IDENTITY_INSERT [dbo].[Menu] ON ' + CHAR(10) + '
INSERT INTO [dbo].[Menu] ' + CHAR(10) + '
(Id ' + CHAR(10) + '
,[Key] ' + CHAR(10) + '
,[DisplayText]' + CHAR(10) + '
,[ParentId]' + CHAR(10) + '
,[CreatedOn])' + CHAR(10) + '
VALUES' + CHAR(10) + '
( ' + ISNULL(CAST(Id AS NVARCHAR(10)), 'NULL') + ' ' + CHAR(10) + '
,''' + ISNULL(CAST([Key] AS NVARCHAR(1000)), 'NULL') +''' ' + CHAR(10) + '
,''' + ISNULL(CAST([Display Text] AS NVARCHAR(1000)), 'NULL') + ''' ' + CHAR(10) + '
,' + ISNULL(CAST(ParentId AS NVARCHAR(10)), 'NULL') + ' ' + CHAR(10) + '
,GETDATE() ' + CHAR(10) + '
) ' + CHAR(10) + '
SET IDENTITY_INSERT [dbo].[Menu] OFF ' + CHAR(10) + '
END ' + CHAR(10) + '
GO ' + CHAR(10) + ' '+ CHAR(10)
FROM @Table
生成的脚本
╔════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ (No column name) ║
╠════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║ IF NOT EXISTS(SELECT 1 FROM [Menu] WHERE Id=1 AND Key='Home') BEGIN SET IDENTITY_INSERT [dbo].[Menu] ON INSERT INTO [dbo].[Menu] (Id ,[Key] ,[DisplayText] ,[ParentId] ,[CreatedOn]) VALUES ( 1 ,'Home' ,'Home' ,NULL ,GETDATE() ) SET IDENTITY_INSERT [dbo].[Menu] OFF END GO ║
║ IF NOT EXISTS(SELECT 1 FROM [Menu] WHERE Id=2 AND Key='About') BEGIN SET IDENTITY_INSERT [dbo].[Menu] ON INSERT INTO [dbo].[Menu] (Id ,[Key] ,[DisplayText] ,[ParentId] ,[CreatedOn]) VALUES ( 2 ,'About' ,'About Us' ,NULL ,GETDATE() ) SET IDENTITY_INSERT [dbo].[Menu] OFF END GO ║
║ IF NOT EXISTS(SELECT 1 FROM [Menu] WHERE Id=3 AND Key='Contact') BEGIN SET IDENTITY_INSERT [dbo].[Menu] ON INSERT INTO [dbo].[Menu] (Id ,[Key] ,[DisplayText] ,[ParentId] ,[CreatedOn]) VALUES ( 3 ,'Contact' ,'Contact Us' ,NULL ,GETDATE() ) SET IDENTITY_INSERT [dbo].[Menu] OFF END GO ║
╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
注意强>
我在Grid
中得到了结果,但您可以将结果导出到文件或文本,并在要执行时将其粘贴到查询窗口中。
答案 1 :(得分:2)
假设我正确理解你的问题,你提出的建议(where子句作为参数)听起来不太好,并且可能导致很多其他问题(例如SQL注入,验证SQL字符串的格式是否正确,等等)。
这种方法如何使用链接服务器
SET IDENTITY_INSERT [dbo].[Menu] ON
GO
INSERT INTO [dbo].[Menu] ([Id],[Key],[DisplayText],[ParentId],[CreatedOn])
SELECT a.Id, a.Key, a.Key, NULL, GETDATE()
FROM [ApplicationMenu_Instance].[ApplicationMenu_Database].[dbo].[ApplicationMenu] AS a
WHERE NOT EXISTS (
SELECT 1
FROM [dbo].[Menu] AS m
WHERE m.Id = a.Id
AND m.Key = a.Key
)
SET IDENTITY_INSERT [dbo].[Menu] OFF
GO
<强>更新强> 既然你想要返回插入脚本,那么动态SQL如何:
CREATE PROCEDURE spGenerateInsertScripts
(
@SourceTable VARCHAR(100),
@TargetTable VARCHAR(100)
)
BEGIN
DECLARE @SQL NVARCHAR(MAX) = '
SET IDENTITY_INSERT [dbo].[Menu] ON
GO
INSERT INTO [dbo].[' + @TargetTable + '] ([Id],[Key],[DisplayText],[ParentId],[CreatedOn])
SELECT a.Id, a.Key, a.Key, NULL, GETDATE()
FROM ' + @SourceTable + ' AS a
WHERE NOT EXISTS (
SELECT 1
FROM [dbo].[' + @TargetTable + '] AS m
WHERE m.Id = a.Id
AND m.Key = a.Key
)
SET IDENTITY_INSERT [dbo].[Menu] OFF
GO
';
SELECT @SQL;
END
答案 2 :(得分:2)
您可以使用SQL语句生成所需的插入语句。然后,您可以将输出复制并粘贴到要执行查询的任何位置。
它不是创建一个脚本的通用解决方案,该脚本从另一个表生成一个表中的插入语句,但它将大大减少您的特定情况所需的手动工作。您可以配置目标表的名称,但是列名称和值以及从中检索数据的表的名称是硬编码的。
它假定输入的目标表与从中检索数据的表具有相同的模式。
DECLARE @TARGET_TABLE AS VARCHAR(100) = '[dbo].[Menu]'
SELECT Script
FROM
(
SELECT Id, [Key], 0 AS [Order],
'IF NOT EXISTS(SELECT 1 FROM ' + @TARGET_TABLE +
' WHERE Id=' + CONVERT(varchar(100), Id) +
' AND Key=''' + [Key] + ''')' AS Script
FROM ApplicationMenu
UNION
SELECT Id, [Key], 1 AS [Order], 'BEGIN' AS Script
FROM ApplicationMenu
UNION
SELECT Id, [Key], 2, 'SET IDENTITY_INSERT ' + @TARGET_TABLE + ' ON'
FROM ApplicationMenu
UNION
SELECT Id, [Key], 3,
'INSERT INTO ' + @TARGET_TABLE +
' VALUES(' +
CONVERT(varchar(11), Id) + ', ''' +
[Key] + ''', ''' +
[DisplayText] + ''', ' +
ISNULL(CONVERT(varchar(11), ParentId), 'NULL') +
', GETDATE())'
FROM ApplicationMenu
UNION
SELECT Id, [Key], 4, 'SET IDENTITY_INSERT ' + @TARGET_TABLE + ' OFF'
FROM ApplicationMenu
UNION
SELECT Id, [Key], 5, 'END'
FROM ApplicationMenu
) AS ScriptInfo
ORDER BY Id, [Key], [Order]
老实说,脚本看起来有点痛苦,但它完成了工作。
如果你真的想要一个通用的问题解决方案,你可能会有更多的运气以某种编程语言(如C#)实现它。在C#中实现它的好处是你可以将库导入SQL服务器并像存储过程一样调用它(我想,我之前从未做过那样的事情)。
此外,还有一些工具可以为您生成此类脚本。如果我没记错的话,RedGate SQL Data Compare会很容易地做到这一点。可能还有其他人。