CREATE PROCEDURE[BoardID]
@sParent varchar(8000)
AS
Select Boardid,PONO,ImageStatus
from BoardDetail
Where
Boardid=1 and @sParent
为什么@sParent创建错误如何在这里使用这个变量......我想提供字符串作为参数,它将设置在哪里......如 @sParent
答案 0 :(得分:2)
听起来你真正想要的是创建一个动态的sql语句,如果这是你想要的,那么试试这个:
Create Procedure [BoardID]
@sParent varchar(8000)
AS
Declare @SQL VarChar(8500)
SELECT @SQL = 'Select Boardid,PONO,ImageStatus from BoardDetail Where
Boardid=1 and '
SELECT @SQL = @SQL + @sParent
Exec ( @SQL)
GO
答案 1 :(得分:1)
您需要在WHERE子句中为@sParent参数设置一个字段:
...
WHERE Boardid = 1 AND [fieldNameThatIsMatchingSParent] = @sParent
...
或类似。
答案 2 :(得分:1)
如果你想将where子句传递给你的sp,请尝试:
CREATE PROCEDURE[BoardID]
@sParent varchar(8000)
AS
DECLARE @sql ntext
SET @sql = 'Select Boardid,PONO,ImageStatus from BoardDetail Where Boardid=1'
IF (@sParent <> '')
BEGIN
SET @sql = @sql + ' and ' + @sParent
END
EXEC @sql
答案 3 :(得分:0)
WHERE
((@Boardid is null and 1=1)
OR
(@Boardid is not null and @Boardid = SomeTbl.Boardid))
这个WHERE子句允许我们拥有一个可选的SQL参数,而不必求助于动态构造的SQL。 希望有所帮助!
答案 4 :(得分:0)
您可以在2种方式中使用@sParent(根据您的需要)
where [FieldName] = @sParent
或
where [FieldName] Like @sParent
答案 5 :(得分:0)
CREATE PROCEDURE[BoardID]
@sParent VARCHAR(8000)
AS
SELECT Boardid, PONO, ImageStatus
FROM BoardDetail
WHERE
Boardid = 1
AND ('0' != @sParent AND [ColumnName] LIKE @sParent)