我在执行查询时遇到上述错误,这是一个存储过程命令行执行
exec sp_Bind_Division_Store_Camera_On_Filter_In_Liveview
null, null, null, null
存储过程如下
ALTER PROCEDURE [dbo].[sp_Bind_Division_Store_Camera_On_Filter_In_Liveview]
@division varchar(45),
@store varchar(45),
@camera varchar(68),
@group varchar(100)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE
@BaseQuery nvarchar(max) = 'select
distinct tblcameradetails.name as CameraName,
tblcameradetails.IsDeviceEnabled,
tblcameradetails.cameraID,
tblcameradetails.deviceIP,
tblmediasourcedetails.StreamName,
tblmediasourcedetails.streamID,
tblcameradetails.Model_ID,
tblcameradetails.IsHidden,
tblcameradetails.hasPTZcapability,
tblcameradetails.CameraModelNo,
tblcameradetails.username as CameraUserName,
tblcameradetails.hasPTZCycle,
tblcameradetails.hasPreset,
tblcameradetails.password as CameraPassword,
tblmediasourcedetails.isRecordingStarted as IsRecordingOn,
tblcameradetails.IsCovert,
tblcameradetails.constCameraName,
tblmediasourcedetails.constStreamName,
tblstoredetails.Store_ID,
tblsystemlocationdetails.division,
tblstoredetails.Store_Name,
tblstoredetails.Store_IP,
tblstoreconfiguration.Liveview_Session_Timeout
from
tblmediasourcedetails,
tblcameradetails,
tblcameragroupdetails,
tblgroupdetails,
tblvssaddsourcedetails,
tblsystemlocationdetails,
tblstoredetails,
tblstoreconfiguration'
, @ParamList nvarchar(max) = N'@p1 varchar(45), @p2 varchar(45), @p3 varchar(68), @p4 varchar(100)'
, @WhereClause nvarchar(max) = N'where
tblmediasourcedetails.cameraID=tblcameradetails.cameraID
and tblmediasourcedetails.streamID=tblvssaddsourcedetails.streamID
and tblcameradetails.cameraID = tblcameragroupdetails.cameraID
and tblcameragroupdetails.groupID=tblgroupdetails.groupID
and tblstoredetails.Store_ID= tblcameradetails.Store_ID
and tblsystemlocationdetails.Store_ID= tblstoredetails.Store_ID
and tblstoredetails.Store_ID=tblstoreconfiguration.Store_ID and 1=1'
, @OrderByClause nvarchar(100) = 'order by CameraName';
IF @division IS NOT NULL
BEGIN
SET @WhereClause = @WhereClause + ' and division like (@p1)';
END
IF @store IS NOT NULL
BEGIN
SET @WhereClause = @WhereClause + ' and (store like (@p2))';
END
IF @camera IS NOT NULL
BEGIN
SET @WhereClause = @WhereClause + ' and tblcameradetails.name like (@p3)';
END
IF @group IS NOT NULL
BEGIN
SET @WhereClause = @WhereClause + ' and tblgroupdetails.name in (@p4)';
END
SET @BaseQuery = @BaseQuery + @WhereClause + @OrderByClause;
EXECUTE sp_executesql @BaseQuery, @ParamList, @p1 = @division, @p2 = @store, @p3 = @camera, @p4 = @group;
END
现在错误显示我在第34行错误的语法附近'。',但我发现没有任何问题,因为我运行此查询表单命令提示符非常好地应用于存储过程,它会引起错误。请帮我解决这个问题。
答案 0 :(得分:1)
您可以轻松打印变量@BaseQuery
以检查生成的查询。
变量@BaseQuery
中的查询看起来像这样
select
distinct tblcameradetails.name as CameraName,
tblcameradetails.IsDeviceEnabled,
tblcameradetails.cameraID,
tblcameradetails.deviceIP,
tblmediasourcedetails.StreamName,
tblmediasourcedetails.streamID,
tblcameradetails.Model_ID,
tblcameradetails.IsHidden,
tblcameradetails.hasPTZcapability,
tblcameradetails.CameraModelNo,
tblcameradetails.username as CameraUserName,
tblcameradetails.hasPTZCycle,
tblcameradetails.hasPreset,
tblcameradetails.password as CameraPassword,
tblmediasourcedetails.isRecordingStarted as IsRecordingOn,
tblcameradetails.IsCovert,
tblcameradetails.constCameraName,
tblmediasourcedetails.constStreamName,
tblstoredetails.Store_ID,
tblsystemlocationdetails.division,
tblstoredetails.Store_Name,
tblstoredetails.Store_IP,
tblstoreconfiguration.Liveview_Session_Timeout
from
tblmediasourcedetails,
tblcameradetails,
tblcameragroupdetails,
tblgroupdetails,
tblvssaddsourcedetails,
tblsystemlocationdetails,
tblstoredetails,
tblstoreconfigurationwhere
tblmediasourcedetails.cameraID=tblcameradetails.cameraID
and tblmediasourcedetails.streamID=tblvssaddsourcedetails.streamID
and tblcameradetails.cameraID = tblcameragroupdetails.cameraID
and tblcameragroupdetails.groupID=tblgroupdetails.groupID
and tblstoredetails.Store_ID= tblcameradetails.Store_ID
and tblsystemlocationdetails.Store_ID= tblstoredetails.Store_ID
and tblstoredetails.Store_ID=tblstoreconfiguration.Store_ID and 1=1order by CameraName
您可以看到WHERE
和ORDER BY
子句之前缺少空格
您需要在WHERE
和ORDER BY
变量的开头添加一个空格,如下所示
@WhereClause nvarchar(max) = N' where
@OrderByClause nvarchar(100) = ' order by CameraName';
答案 1 :(得分:0)
尝试将一些PRINT
放在查询的中间。
例如PRINT @BaseQuery
之后的SET @BaseQuery = @BaseQuery + @WhereClause + @OrderByClause;
因为某些变量可能需要空格(例如N' where...1=1'
而不是N'where...1=1'
)