我的存储过程:
@type varchar(250) = NULL
AS
Begin
DECLARE @Sql NVARCHAR(MAX);
SELECT @Sql = N'SELECT * FROM Match WHERE MatchID Between '
+ CONVERT(NVARCHAR(50),(@currPage - 1)*@recodperpage+1)+
' and '+ CONVERT(NVARCHAR(50),@currPage*@recodperpage)
IF @type IS NOT NULL
SELECT @Sql += N' AND Type = ' + convert(varchar(50),@type)
EXEC SP_EXECUTESQL @Sql
END
我执行但得到错误
DECLARE @return_value int
EXEC @return_value = [dbo].[search_PhanTrang]
@currPage = 1,
@recodperpage = 5,
@Pagesize = 6,
@type = N'random'
SELECT 'Return Value' = @return_value
错误:
Msg 207,Level 16,State 1,Line 1
列名称无效'随机'。
如何添加'到我的SQL字符串来修复此错误? (' + @ type +')
答案 0 :(得分:2)
正如您可能已经知道的那样,字符串需要放在单引号之间。此外,如果类型包含引号,则需要将其加倍。这使得以下行:
public mycls()
{
InitializeComponent()
(this.FindName("mybtn") as Button).Click += mybtn_click;
}