将参数传递给 SQL 服务器存储过程

Passing parameter to a SQL Server stored procedure

我正在尝试编写一个 SQL 查询并且它可以工作,但是当我尝试使用参数时存储过程不工作。

示例:

select * 
from Table1 
where Name = 'BEST People'     // this works

// this does not show any rows
declare @Name nvarchar(128)
set @Name= 'BEST People'

select * 
from Table1 
where Name = @Name

即使我尝试使用存储过程,它也不起作用。

您需要将字符串转换为 NVARCHAR:

declare @Name nvarchar(128)
set @Name= N'BEST People'
select * from Table1 where Name = @Name

没有 N 它将是 VARCHAR

取决于您的列排序规则字符串比较工作 像这样说明你的问题

检查列排序规则的方法如下:

DECLARE @TableId INT
SELECT @TableId=id FROM sys.sysobjects
    WHERE xtype='U' AND name='Table1';  --Your table name here
SELECT name, collation_name FROM sys.columns
    WHERE object_id=@TableId AND name=N'Name';  --Your column name here

View Collation Information