条件不起作用的存储过程

时间:2015-05-05 09:19:21

标签: sql sql-server-2008 stored-procedures

这是我从表中选择数据的存储过程。但如果我把条件放在其中。然后它抛出一个错误

  

“'='附近的语法不正确。”

但是,如果我删除where条件,那么这个查询将正常工作。任何人都可以告诉我,我在哪里做错了什么。

alter procedure [dbo].[GetCompleteCPTDetails]
    @Practice_Short_Name varchar(50) = null,
    @Uploaded_Date varchar(max) = null
as
begin
DECLARE @CPTtablename varchar(100)
DECLARE @vQuery NVARCHAR(max)
set @CPTtablename = 'ACER_CLAIMS_MASTER_DETAIL_Hist_'+@Practice_Short_Name+''
SET @vQuery = 'select Practice_Short_Name,Service_Date_From,Carrier_Name,
   Location_Description,Patient_Number,Patient_First_Name,
   Patient_Last_Name,Voucher_Number,Procedure_Code,Service_Fees,
   Service_Payments,Service_Adjustments,Acer_Status,Acer_Allowed_Amount    
from ' + @CPTtablename + 'where Uploaded_Date ='+ @Uploaded_Date +'
order by acer_status asc, Service_Date_From desc, Patient_First_Name asc'

EXEC (@vQuery)

end
GO

1 个答案:

答案 0 :(得分:0)

对于日期@Uploaded_Date,您需要如下配额

alter procedure [dbo].[GetCompleteCPTDetails]
    @Practice_Short_Name varchar(50) = null,
    @Uploaded_Date varchar(max) = null
as
begin
DECLARE @CPTtablename varchar(100)
DECLARE @vQuery NVARCHAR(max)
set @CPTtablename = 'ACER_CLAIMS_MASTER_DETAIL_Hist_'+@Practice_Short_Name+''
SET @vQuery = 'select Practice_Short_Name,Service_Date_From,Carrier_Name,
   Location_Description,Patient_Number,Patient_First_Name,
   Patient_Last_Name,Voucher_Number,Procedure_Code,Service_Fees,
   Service_Payments,Service_Adjustments,Acer_Status,Acer_Allowed_Amount    
from ' + @CPTtablename + ' where Uploaded_Date ='''+ @Uploaded_Date +'''
order by acer_status asc, Service_Date_From desc, Patient_First_Name asc'

EXEC (@vQuery)

end
GO