使用参数在SQL Server中创建和执行存储过程

时间:2016-12-20 13:55:58

标签: sql-server tsql stored-procedures

我有四张桌子:

  • dbo.Projects(id,ProjectName,Areas,PaymentSystem,Districts.id,purpose.id,types.id等)
  • dbo.Districts(id,DistrictsName)
  • dbo.Purpose(id,PurposeName) - 有住宅&商业
  • dbo.Types(id,typName)

我想选择DistrictsName =' District1'和PurposeName ='住宅'

我尝试了这个程序:

alter PROCEDURE GetProjects
    @districtName nvarchar(50),
    @purposeName nvarchar(50)
as
    SELECT 
        p.ID, p.ProjectName,  p.Areas, 
        p.PaymentSystem, p.ReceivedDate,    
        p.PropertyClassification, 
        p.ProjectImage,         
        dis.DistrictName,
        Pur.PurposeName            
    FROM 
        dbo.Projects AS p 
    LEFT JOIN 
        dbo.Districts dis ON p.DistrictID = dis.ID   
    LEFT JOIN  
        dbo.Purpose pur ON p.PurposeID = pur.ID 
    WHERE 
       (dis.DistrictName = @districtName) 
       AND (Pur.PurposeName = @purposeName)

exec GetProjects @districtName='District1', @purposeName='residential '

当我执行此过程时,它返回null - 不返回任何数据。

注意:数据库支持的阿拉伯语

当我执行此查询时,它返回结果我认为没有返回数据,因为阿拉伯语

SELECT 
    p.ID, p.ProjectName, p.Areas, 
    p.PaymentSystem, p.ReceivedDate,    
    p.PropertyClassification, 
    p.ProjectImage,         
    dis.DistrictName,
    Pur.PurposeName            
    FROM 
        dbo.Projects AS p 
    LEFT JOIN 
        dbo.Districts dis ON p.DistrictID = dis.ID   
    LEFT JOIN  
        dbo.Purpose pur ON p.PurposeID = pur.ID 
    WHERE 
        (dis.DistrictName = N'arabic value') 
        AND (Pur.PurposeName = N'arabic value')

1 个答案:

答案 0 :(得分:1)

使用此执行命令

exec GetProjects @districtName=N'arabic value',@purposeName=N'arabic    value'

将N放在前面以表示您正在使用Unicode。