使用SQL Server并在SET @sql命令中获取语法错误

时间:2018-12-03 19:31:15

标签: sql-server

.PrintHex()

2 个答案:

答案 0 :(得分:3)

create table #mkg_metrics语句的末尾没有右括号。

create table #mkg_metrics (TR_EM_NO char(11),TR_DATE_TIME datetime,TRAN_TYPE char(4),QUANTITY numeric(10,0),TR_STAGE_LOCATION char(15),TR_loca char(10),DOCK char(2),
VD_NAME varchar(36))

答案 1 :(得分:0)

SQL DEMO

对字符串文字使用单引号,并两次对单引号进行转义

declare @sql varchar(max);
set @sql = '
insert into #mkg_metrics
    select TR_EM_NO, TR_DATE_TIME, TRAN_TYPE, QUANTITY, TR_STAGE_LOCATION, TR_loca
    from LOGTRAN inner join TRAILER_LOG tl
    where TR_DATE_TIME >= DATEADD(DAY, -1, getdate()) and TR_DATE_TIME <= GETDATE() and tl.TRAN_TYPE = ''SETUP''

insert into #mkg_metrics
    select tl.DOCK
    from LOGTRAN inner join TRAILER_LOG tl
    where TR_DATE_TIME >= DATEADD(DAY, -1, getdate()) and TR_DATE_TIME <= GETDATE() and tl.TRAN_TYPE = ''SETUP''

insert into #mkg_metrics
    select vm.VD_NAME
    from LOGTRAN left outer join IMMAS im
    on im.VD_NO = vm.VD_NO left outer join TRAILER_LOG tl
    where TR_DATE_TIME >= DATEADD(DAY, -1, getdate()) and TR_DATE_TIME <= GETDATE() and tl.TRAN_TYPE = ''SETUP''
';

SELECT @sql;