每个SQL select语句的结果均为文本格式

时间:2019-01-03 03:27:49

标签: sql-server

我有select语句,该语句导致数千行。

 SELECT l.account_reference_no as arn, 
       Coalesce(l.preApprovalNo,0) as app_no, 
       Coalesce(Dateadd(SECOND, l.time_stamp_seconds, l.time_stamp_date), '1900-01-01 00:00:00') as prosp_date, 
       l.combined_name as name, 
       status    FROM   Link_LIST as l

此处给出了一些示例结果

arn         app_no      prosp_date              name                                                                                                  status
----------- ----------- ----------------------- ----------------------------------------------------------------------------------------------------- --------
100110018   0           1900-01-01 00:00:00.000 WG & SM Davis                                                                                         INACTIVE
100110026   0           1900-01-01 00:00:00.000 Mrs LM Johnston                                                                                       INACTIVE
100110034   0           1900-01-01 00:00:00.000 P & V Bartlett & Giles                                                                                INACTIVE
100110042   0           1900-01-01 00:00:00.000 Mr D & Ms L Setters & Kirley                                                                          INACTIVE
100110059   0           1900-01-01 00:00:00.000 CT & DE West                                                                                          INACTIVE
100110067   0           1900-01-01 00:00:00.000                                                                                                       INACTIVE
100110075   0           1900-01-01 00:00:00.000 A & L O'Bree                                                                                          INACTIVE
100110091   0           1900-01-01 00:00:00.000 RJ & LE Schaeche & Kenny                                                                              INACTIVE

我正在寻找一种解决方案,以获取以下格式的输出。这是因为,我必须将此结果插入到另一个系统上的另一个数据库中

insert into #new_arn (arn, app_no, prosp_date, name, status) values (402470011,0, '01/Jan/1900 00:00:00','Mr A Bicket','Inactive')
insert into #new_arn (arn, app_no, prosp_date, name, status) values (402470037,0, '01/Jan/1900 00:00:00','Ms K Edwards','Inactive')
insert into #new_arn (arn, app_no, prosp_date, name, status) values (402470052,0, '01/Jan/1900 00:00:00','Ms BJ Sippel','Inactive')
insert into #new_arn (arn, app_no, prosp_date, name, status) values (402470110,0, '01/Jan/1900 00:00:00','Mr GA & Mrs S Wilson','Inactive')
insert into #new_arn (arn, app_no, prosp_date, name, status) values (402470128,0, '01/Jan/1900 00:00:00','r M & Ms S Holland','Inactive')
insert into #new_arn (arn, app_no, prosp_date, name, status) values (402470144,0, '01/Jan/1900 00:00:00','Mr M Hanns','Inactive')

有没有一种方法可以使用MSSQL Management Studio生成

2 个答案:

答案 0 :(得分:2)

尝试以下操作:

select 'insert into #new_arn (arn, app_no, prosp_date, name, status) values ( ' + convert(varchar(max), l.account_reference_no) + ','+  convert(varchar(max), Coalesce(l.preApprovalNo,0)) 
       + ',''' + Coalesce(Dateadd(SECOND, l.time_stamp_seconds, l.time_stamp_date), '1900-01-01 00:00:00') + ''',''' +  l.combined_name + ''',' + '''Inactive''' from [dbo].[sourceTable]

先打CTRL+T(文本输出),然后再打CTRL+E(执行查询)

请根据您的要求设置日期格式。

答案 1 :(得分:0)

谢谢大家的支持。这是最终的解决方案,以防将来有人需要参考

SELECT CONCAT('insert into #new_arn (arn, app_no, prosp_date, name, status) values (', l.account_reference_no,',',Coalesce(l.preApprovalNo,0),',''',Coalesce(Dateadd(SECOND, l.time_stamp_seconds, l.time_stamp_date), '1900-01-01 00:00:00'),''', ''',l.combined_name,''',''',status,''')') FROM   Link_LIST as l