第 11 行“2”附近的语法不正确。第 11 行是“@processdate”变量
Incorrect syntax near '2' at line 11. line 11 is "@processdate" variable
我在@processdate 变量中遇到语法错误,根据我的理解,这是因为日期格式(存储在@processdate 变量中的子查询)。我需要帮助,因为我是 sql.
的新手
我正在尝试在存储过程中设置所有查询。我尝试过的 - 不同的日期格式,转换@processdate 变量。
ALTER PROCEDURE [dbo].[rec_search_pay]
@processdatefrom DATETIME,
@processdateto DATETIME,
@collectiondatefrom DATETIME,
@collectiondateto DATETIME,
@amount MONEY,
@stationid VARCHAR(10),
@bankcode VARCHAR(2),
@branchcode VARCHAR(6),
@ponumber VARCHAR(10)
AS
BEGIN
DECLARE @bank AS VARCHAR(MAX);
DECLARE @branch AS VARCHAR(MAX);
DECLARE @station AS VARCHAR(MAX);
DECLARE @amounts AS VARCHAR(MAX);
DECLARE @pono AS VARCHAR(MAX);
DECLARE @processdate AS NVARCHAR(MAX);
DECLARE @collectiondate AS NVARCHAR(MAX);
DECLARE @stmt AS VARCHAR(MAX);
DECLARE @linebreak AS VARCHAR(2);
SET @linebreak = CHAR(13) + CHAR(10);
SET @processdate = ' AND h.processdate >= ' + CAST(@processdatefrom AS NVARCHAR) +
' AND h.processdate <= ' + CAST(@processdateto AS NVARCHAR)
SET @collectiondate = ' AND h.collectiondate >= ' + CAST(@collectiondatefrom AS NVARCHAR) +
' AND h.collectiondate <= ' + CAST(@collectiondateto AS NVARCHAR)
IF (@amount LIKE '%[^0-9]')
SET @amounts = ' AND amount = ' + @amount
ELSE
SET @amounts = ' '
IF (@bankcode <> '00')
SET @bank = ' and h.bankcode = ' + @bankcode
ELSE
SET @bank = ' '
IF (@branchcode <> '0')
SET @branch = ' and h.branchcode = ' + @branchcode
ELSE
SET @branch = ' '
IF (@stationid <> 7)
SET @station = ' and h.stationid = ' + @stationid
ELSE
SET @station = ' '
IF (@ponumber LIKE '%[^0-9]')
SET @pono = ' and h.advicenumber = ' + @ponumber
ELSE
SET @pono = ' '
SET NOCOUNT ON;
SET @stmt = 'SELECT h.bankcode, RTRIM(bankname) AS bankname, h.branchcode,
RTRIM(branchname) AS branchname, h.processdate,
h.collectiondate, h.advicenumber, h.amount, h.commission,
h.servicecharges, h.others, h.adjustment, s.stationname, h.userid
FROM banks b, branches br, reconcile h, stations s
WHERE CAST(h.bankcode AS VARCHAR) = b.bankcode
AND CAST(h.bankcode AS VARCHAR) = br.bankcode
AND CAST(h.branchcode AS VARCHAR) = br.branchcode
AND h.stationid = s.stationid
AND returned <> ''Y''
'+ @processdate + @linebreak + --problems seems to be here
+ @collectiondate + @linebreak +
+ @bank + @linebreak +
+ @branch + @linebreak +
+ @station + @linebreak +
+ @amounts + @linebreak +
+ @pono + @linebreak +
'ORDER BY 1, 3, collectiondate'
EXEC (@stmt)
正在执行存储过程:
exec rec_search_pay '2018-mar-02', '2018-may-01', '2018-mar-01',
'2018-apr-30', 27698, 1, 3, 2003, 4721621
预期结果:
3 /NATIONAL BANK OF PAKISTAN /2003 /C.O.D. DRIGH ROAD [2003]
/2018-04-05 00:00:00.000 /2018-04-02 00:00:00.000 /4721621 /27698.00
/272.00 /0.00 /0.00 /0.00 /Karachi /john
实际结果:
Incorrect syntax near '2'.
使用打印的存储过程(查询):
select h.bankcode, rtrim(bankname) AS bankname, h.branchcode,
rtrim(branchname) AS branchname, h.processdate, h.collectiondate,
h.advicenumber, h.amount, h.commission, h.servicecharges, h.others,
h.adjustment, s.stationname, h.userid
from banks b, branches br, reconcile h, stations s
Where cast (h.bankcode AS VARCHAR) = b.bankcode
and cast (h.bankcode AS VARCHAR) = br.bankcode
and cast (h.branchcode AS VARCHAR) = br.branchcode
and h.stationid = s.stationid
and returned <> 'Y'
and h.processdate >= Mar 2 2018 12:00AM and h.processdate <= --prob
May 1 2018 12:00AM
and h.collectiondate >= Mar 1 2018 12:00AM and h.collectiondate <=
Apr 30 2018 12:00AM
and h.bankcode = 3
and h.branchcode = 2003
and h.stationid = 1
order by 1, 3, collectiondate
当您在查询中指定日期文字时,日期值必须用单引号引起来。例如这部分代码:
and h.processdate >= Mar 2 2018 12:00AM
如果添加引号,它应该变成:
and h.processdate >= 'Mar 2 2018 12:00AM'
但是使用这样的日期格式并不是一个好主意。 2018 年 3 月 2 日最好使用 '20180302'
。这样可以省去很多麻烦。
我在@processdate 变量中遇到语法错误,根据我的理解,这是因为日期格式(存储在@processdate 变量中的子查询)。我需要帮助,因为我是 sql.
的新手我正在尝试在存储过程中设置所有查询。我尝试过的 - 不同的日期格式,转换@processdate 变量。
ALTER PROCEDURE [dbo].[rec_search_pay]
@processdatefrom DATETIME,
@processdateto DATETIME,
@collectiondatefrom DATETIME,
@collectiondateto DATETIME,
@amount MONEY,
@stationid VARCHAR(10),
@bankcode VARCHAR(2),
@branchcode VARCHAR(6),
@ponumber VARCHAR(10)
AS
BEGIN
DECLARE @bank AS VARCHAR(MAX);
DECLARE @branch AS VARCHAR(MAX);
DECLARE @station AS VARCHAR(MAX);
DECLARE @amounts AS VARCHAR(MAX);
DECLARE @pono AS VARCHAR(MAX);
DECLARE @processdate AS NVARCHAR(MAX);
DECLARE @collectiondate AS NVARCHAR(MAX);
DECLARE @stmt AS VARCHAR(MAX);
DECLARE @linebreak AS VARCHAR(2);
SET @linebreak = CHAR(13) + CHAR(10);
SET @processdate = ' AND h.processdate >= ' + CAST(@processdatefrom AS NVARCHAR) +
' AND h.processdate <= ' + CAST(@processdateto AS NVARCHAR)
SET @collectiondate = ' AND h.collectiondate >= ' + CAST(@collectiondatefrom AS NVARCHAR) +
' AND h.collectiondate <= ' + CAST(@collectiondateto AS NVARCHAR)
IF (@amount LIKE '%[^0-9]')
SET @amounts = ' AND amount = ' + @amount
ELSE
SET @amounts = ' '
IF (@bankcode <> '00')
SET @bank = ' and h.bankcode = ' + @bankcode
ELSE
SET @bank = ' '
IF (@branchcode <> '0')
SET @branch = ' and h.branchcode = ' + @branchcode
ELSE
SET @branch = ' '
IF (@stationid <> 7)
SET @station = ' and h.stationid = ' + @stationid
ELSE
SET @station = ' '
IF (@ponumber LIKE '%[^0-9]')
SET @pono = ' and h.advicenumber = ' + @ponumber
ELSE
SET @pono = ' '
SET NOCOUNT ON;
SET @stmt = 'SELECT h.bankcode, RTRIM(bankname) AS bankname, h.branchcode,
RTRIM(branchname) AS branchname, h.processdate,
h.collectiondate, h.advicenumber, h.amount, h.commission,
h.servicecharges, h.others, h.adjustment, s.stationname, h.userid
FROM banks b, branches br, reconcile h, stations s
WHERE CAST(h.bankcode AS VARCHAR) = b.bankcode
AND CAST(h.bankcode AS VARCHAR) = br.bankcode
AND CAST(h.branchcode AS VARCHAR) = br.branchcode
AND h.stationid = s.stationid
AND returned <> ''Y''
'+ @processdate + @linebreak + --problems seems to be here
+ @collectiondate + @linebreak +
+ @bank + @linebreak +
+ @branch + @linebreak +
+ @station + @linebreak +
+ @amounts + @linebreak +
+ @pono + @linebreak +
'ORDER BY 1, 3, collectiondate'
EXEC (@stmt)
正在执行存储过程:
exec rec_search_pay '2018-mar-02', '2018-may-01', '2018-mar-01',
'2018-apr-30', 27698, 1, 3, 2003, 4721621
预期结果:
3 /NATIONAL BANK OF PAKISTAN /2003 /C.O.D. DRIGH ROAD [2003]
/2018-04-05 00:00:00.000 /2018-04-02 00:00:00.000 /4721621 /27698.00
/272.00 /0.00 /0.00 /0.00 /Karachi /john
实际结果:
Incorrect syntax near '2'.
使用打印的存储过程(查询):
select h.bankcode, rtrim(bankname) AS bankname, h.branchcode,
rtrim(branchname) AS branchname, h.processdate, h.collectiondate,
h.advicenumber, h.amount, h.commission, h.servicecharges, h.others,
h.adjustment, s.stationname, h.userid
from banks b, branches br, reconcile h, stations s
Where cast (h.bankcode AS VARCHAR) = b.bankcode
and cast (h.bankcode AS VARCHAR) = br.bankcode
and cast (h.branchcode AS VARCHAR) = br.branchcode
and h.stationid = s.stationid
and returned <> 'Y'
and h.processdate >= Mar 2 2018 12:00AM and h.processdate <= --prob
May 1 2018 12:00AM
and h.collectiondate >= Mar 1 2018 12:00AM and h.collectiondate <=
Apr 30 2018 12:00AM
and h.bankcode = 3
and h.branchcode = 2003
and h.stationid = 1
order by 1, 3, collectiondate
当您在查询中指定日期文字时,日期值必须用单引号引起来。例如这部分代码:
and h.processdate >= Mar 2 2018 12:00AM
如果添加引号,它应该变成:
and h.processdate >= 'Mar 2 2018 12:00AM'
但是使用这样的日期格式并不是一个好主意。 2018 年 3 月 2 日最好使用 '20180302'
。这样可以省去很多麻烦。