sql语句有一种特别的用法,相信很多人还不知道,我也是后来学到的,因此拿来跟大家分享下:生成sql脚本
举个例子:
select 1 from student;
如果表中有数据的话,你就会发现查询的结果会是:
根据这个原理,我们可以用来生成sql脚本,以下就是实例:
实例1:一个简单的例子
select * from Popedom
select 'insert intoPopedom(id,popedom_name,popedom_value) values
('+cast(id as varchar(10))+',''' +popedom_name + ''',
''' + popedom_value+ ''')' FROM Popedom
insert intoPopedom(id,popedom_name,popedom_value) values (1,'电厂一览', 'PRIV_ELECTINFO')
insert intoPopedom(id,popedom_name,popedom_value) values (2,'概算信息', 'PRIV_BUDGET')
insert intoPopedom(id,popedom_name,popedom_value) values (3,'合同信息', 'PRIV_CONTRACT')
insert intoPopedom(id,popedom_name,popedom_value) values (4,'投资信息', 'PRIV_INVESTMENT')
insert intoPopedom(id,popedom_name,popedom_value) values (5,'进度信息', 'PRIV_PROCESS')
insert intoPopedom(id,popedom_name,popedom_value) values (6,'安全信息', 'PRIV_SAFETY')
insert intoPopedom(id,popedom_name,popedom_value) values (7,'日常工作分配', 'PRIV_SETUP')
insert intoPopedom(id,popedom_name,popedom_value) values (8,'日常工作浏览', 'PRIV_AWAKE')
insert intoPopedom(id,popedom_name,popedom_value) values (9,'日常工作录入', 'PRIV_TASK')
insert intoPopedom(id,popedom_name,popedom_value) values (10,'提醒基本信息设置', 'PRIV_BASE')
insert intoPopedom(id,popedom_name,popedom_value) values (11,'个人信息设置', 'PRIV_PERSON')
insert intoPopedom(id,popedom_name,popedom_value) values (12,'超级管理员', 'PRIV_ADMIN')
insert intoPopedom(id,popedom_name,popedom_value) values (13,'设计信息', 'PRIV_DESIGN')
insert intoPopedom(id,popedom_name,popedom_value) values (14,'质量信息', 'PRIV_QUALITY')
insert intoPopedom(id,popedom_name,popedom_value) values (15,'日常工作汇报', 'PRIV_REPORT')
insert intoPopedom(id,popedom_name,popedom_value) values (16,'日常工作审核', 'PRIV_CHECK')
insert intoPopedom(id,popedom_name,popedom_value) values (17,'电厂信息录入', 'PRIV_ElECTINFO')
insert intoPopedom(id,popedom_name,popedom_value) values (18,'设计信息录入', 'PRIV_DESIGNINFO')
实例2:相对复杂一点的例子
SELECT 'INSERT INTOPrevise(PreviseID,PreviseDate,PreviseInfo) VALUES
('+cast(PreviseID as varchar(10))+',''' +CONVERT(varchar(19), PreviseDate, 121) + ''',
''' + PreviseInfo + ''')' FROM PreviseWHERE PreviseID > 100