ISPrincess
Programmer
Following is my exact statement in stored proc.
Syntax checks ok but get errors on execute in query analyser:
It must be my use of quotation marks etc. Please help
STORED PROC:
CREATE PROCEDURE dbo.rpt_Link_Test
@sScheduledDate varchar(8),
@sPrestageShift varchar(2),
@sLineName varchar(2),
@sToFac varchar(1)
AS
DECLARE @sSQL nVarchar(4000)
DECLARE @sFac Varchar(3)
Set @sFac = 'NAP'
Set @sSQL = 'Select * from openquery(Server1, ''SELECT DISTINCT '' + @sFac + '' as Facility,
UCM.MoveNo,
UCM.WMSMoveType,
LPH.LicensePlateNo,
UCM.FormattedFromLocation,
UCM.FormattedToLocation,
UCM.FromToFacility,
UCM.ScheduledDate,
UCM.PrestageShift,
UCM.LineName,
UCM.MoveQuantity,
UCM.Code + ' + '-' + ' + UCM.Choice + ' + '-' + ' + UCM.Alternate as SKU
From Server1.FacilityA.dbo.LPHeaderHist LPH, Server1.FacilityA.dbo.LPDetailHist LPD, Server1.FacilityA.dbo.UCMHist UCM
Where LPH.LicensePlateNo = LPD.LicensePlateNo
and LPD.MoveNo = UCM.MoveNo and LPD.WMSMoveType = UCM.WMSMoveType
and UCM.MoveType = ' + '06' +
'and UCM.Scheduleddate = ' + @sScheduledDate +
'and UCM.Prestageshift = ' + @sPrestageShift +
'and UCM.LineName = ' + @sLineName +
'and UCM.FromToFacility = ' + 'N' +@sToFac +
'and LPH.LicensePlateNo Not In
(select LPH2.LicensePlateNo
from Server1.FacilityA.dbo.LPHeaderHist LPH2
Where LPH2.LicensePlateNo = LPH.LicensePlateNo
and (Right(RTrim(LPH2.CurrentLocation),2) = '+ '99' + '
or LPH2.TransStatus = ' + '5105' + ')'''')'
EXEC sp_executesql @sSQL
ERROR:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '+'.
Server: Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark before the character string ' as Facility,
UCM.MoveNo,
UCM.WMSMoveType,
LPH.LicensePlateNo,
UCM.FormattedFromLocation,
UCM.FormattedToLocation,
UCM.FromToFacility,
UCM.ScheduledDate,
UCM.PrestageShift,
UCM.LineName,
UCM.MoveQuantity,
UCM.Code + - + UCM.Choice + - + UCM.Alternate as SKU
From Server1.FacilityA.dbo.LPHeaderHist LPH, Server1.FacilityA.d...
Syntax checks ok but get errors on execute in query analyser:
It must be my use of quotation marks etc. Please help
STORED PROC:
CREATE PROCEDURE dbo.rpt_Link_Test
@sScheduledDate varchar(8),
@sPrestageShift varchar(2),
@sLineName varchar(2),
@sToFac varchar(1)
AS
DECLARE @sSQL nVarchar(4000)
DECLARE @sFac Varchar(3)
Set @sFac = 'NAP'
Set @sSQL = 'Select * from openquery(Server1, ''SELECT DISTINCT '' + @sFac + '' as Facility,
UCM.MoveNo,
UCM.WMSMoveType,
LPH.LicensePlateNo,
UCM.FormattedFromLocation,
UCM.FormattedToLocation,
UCM.FromToFacility,
UCM.ScheduledDate,
UCM.PrestageShift,
UCM.LineName,
UCM.MoveQuantity,
UCM.Code + ' + '-' + ' + UCM.Choice + ' + '-' + ' + UCM.Alternate as SKU
From Server1.FacilityA.dbo.LPHeaderHist LPH, Server1.FacilityA.dbo.LPDetailHist LPD, Server1.FacilityA.dbo.UCMHist UCM
Where LPH.LicensePlateNo = LPD.LicensePlateNo
and LPD.MoveNo = UCM.MoveNo and LPD.WMSMoveType = UCM.WMSMoveType
and UCM.MoveType = ' + '06' +
'and UCM.Scheduleddate = ' + @sScheduledDate +
'and UCM.Prestageshift = ' + @sPrestageShift +
'and UCM.LineName = ' + @sLineName +
'and UCM.FromToFacility = ' + 'N' +@sToFac +
'and LPH.LicensePlateNo Not In
(select LPH2.LicensePlateNo
from Server1.FacilityA.dbo.LPHeaderHist LPH2
Where LPH2.LicensePlateNo = LPH.LicensePlateNo
and (Right(RTrim(LPH2.CurrentLocation),2) = '+ '99' + '
or LPH2.TransStatus = ' + '5105' + ')'''')'
EXEC sp_executesql @sSQL
ERROR:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '+'.
Server: Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark before the character string ' as Facility,
UCM.MoveNo,
UCM.WMSMoveType,
LPH.LicensePlateNo,
UCM.FormattedFromLocation,
UCM.FormattedToLocation,
UCM.FromToFacility,
UCM.ScheduledDate,
UCM.PrestageShift,
UCM.LineName,
UCM.MoveQuantity,
UCM.Code + - + UCM.Choice + - + UCM.Alternate as SKU
From Server1.FacilityA.dbo.LPHeaderHist LPH, Server1.FacilityA.d...