Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在exec语句中格式化sql_Sql_Sql Server_Datetime_Sql Server 2008 R2_Exec - Fatal编程技术网

如何在exec语句中格式化sql

如何在exec语句中格式化sql,sql,sql-server,datetime,sql-server-2008-r2,exec,Sql,Sql Server,Datetime,Sql Server 2008 R2,Exec,当我运行以下代码时 declare @startDT datetime declare @endDT datetime set @startDT = '2014-09-18 09:28:15.650' set @endDT = N'2014-09-18 09:28:15.650' declare @tstamp datetime set @tstamp = '05/06/2014 15:08:00' exec('if ' + @startDT + ' <= ' + @tstamp +

当我运行以下代码时

declare @startDT datetime
declare @endDT datetime
set @startDT = '2014-09-18 09:28:15.650'
set @endDT = N'2014-09-18 09:28:15.650'

declare @tstamp datetime
set @tstamp = '05/06/2014  15:08:00'

exec('if ' + @startDT + ' <= ' + @tstamp + '
begin
select ''less than''
end
else
begin
select ''greater than''
end')
但是,当我删除exec时,它可以正常工作。我做错了什么?

声明@startDT datetime
declare @startDT datetime
declare @endDT datetime
set @startDT = '2014-09-18 09:28:15.650'
set @endDT = N'2014-09-18 09:28:15.650'

declare @tstamp datetime
set @tstamp = '05/06/2014  15:08:00'

exec('if ' +'"'+ @tstamp +'"'+ ' <= ' +'"'+ @tstamp +'"'+ '
begin
select ''less than''
end
else
begin
select ''greater than''
end')
声明@endDT datetime set@startDT='2014-09-1809:28:15.650' set@endDT=N'2014-09-1809:28:15.650' 声明@tstamp datetime 设置为@tstamp='05/06/2014 15:08:00'
exec('如果'+''''+@tstamp+''''+'您需要在字符串中形成一个完整的SQL查询,然后将其作为参数传递。因此,在执行@SQL参数之前,字符串需要将日期时间值作为字符串。在@SQL端,然后使用convert(datetime…)以确保所需的比较作为datetime进行

DECLARE @sql     nvarchar(max)
DECLARE @startDT nvarchar(23)
DECLARE @endDT   nvarchar(23)

SET @startDT = N'2014-09-18 09:28:15.650'
SET @endDT   = N'2014-09-18 09:28:15.650'

DECLARE @tstamp nvarchar(23)
SET @tstamp = N'2014-05-06 15:08:00'


SET @sql = N'if convert(datetime,'''
           + @startDT
           + ''',121) <= convert(datetime,'''
           + @tstamp 
           + ''',121) begin
 select ''less than''
 end
 else
 begin
 select ''greater than''
 end'

-- SELECT @sql
-- if convert(datetime,'2014-09-18 09:28:15.650',121) <= convert(datetime,'2014-05-06 15:08:00',121) begin select 'less than' end else begin select 'greater than' end

EXEC(@SQL)

选择“小于”
。从何处选择(未指定表)?我想您是想在那里使用打印。您好,它应该只打印小于或大于。如果我不使用exec,它可以工作。您也是对的,我也可以使用打印。但是,当我执行脚本时,打印将在消息(选项卡)中打印文本,而不是在结果(选项卡)中打印文本。然后将查询更改为
从dual中选择“小于”
这不是oracle语法吗。我没有一个名为dual的表。什么是dual?我刚刚尝试了这个,它可以执行('select'works!''))谢谢。当我运行您的脚本时,我收到以下错误消息207,级别16,状态1,第1行无效列名“May 6 2014 3:08PM”。消息207,级别16,状态1,第1行无效列名“May 6 2014 3:08PM”。但您是对的。这是由于格式错误造成的语法错误。
DECLARE @sql     nvarchar(max)
DECLARE @startDT nvarchar(23)
DECLARE @endDT   nvarchar(23)

SET @startDT = N'2014-09-18 09:28:15.650'
SET @endDT   = N'2014-09-18 09:28:15.650'

DECLARE @tstamp nvarchar(23)
SET @tstamp = N'2014-05-06 15:08:00'


SET @sql = N'if convert(datetime,'''
           + @startDT
           + ''',121) <= convert(datetime,'''
           + @tstamp 
           + ''',121) begin
 select ''less than''
 end
 else
 begin
 select ''greater than''
 end'

-- SELECT @sql
-- if convert(datetime,'2014-09-18 09:28:15.650',121) <= convert(datetime,'2014-05-06 15:08:00',121) begin select 'less than' end else begin select 'greater than' end

EXEC(@SQL)
IF CONVERT(datetime, '2014-09-18 09:28:15.650', 121) <=
      CONVERT(datetime, '2014-05-06 15:08:00', 121)
BEGIN
      SELECT
            'less than'
END
ELSE
BEGIN
      SELECT
            'greater than'
END