日期:2014-05-18  浏览次数:20518 次

求一简单存储过程语句
如下:
表结构如下
CREATE   TABLE   ServiceRate
(   ID   INT,   --不是流水号
    StartDate   DateTime,
    SiteID   INT,
    ServiceID   INT,
    ServiceName   VARCHAR(50)
)
存储过程如下
CREATE       PROCEDURE   Pro_GetServiceRateID(
@nSiteID   INT,
@tStartDate   DateTime,
@strServiceIDs   VARCHAR(255)--ServiceID组合,中间用逗号隔开的


现在想根据传进来的@nSiteID,@tStartDate,@strServiceIDs   与ServiceRate表匹配,如果存在返回ID,没有就返回0
请大家帮帮忙!

------解决方案--------------------
CREATE PROCEDURE Pro_GetServiceRateID
@nSiteID INT,
@tStartDate DateTime,
@strServiceIDs VARCHAR(255)--ServiceID组合,中间用逗号隔开的
AS
set nocount on
select ServiceID from ServiceRate where StartDate = @tStartDate and SiteID = @nSiteID and charindex(cast(ServiceID as varchar), @strServiceIDs) > 0
set nocount off
go

------解决方案--------------------
CREATE PROCEDURE Pro_GetServiceRateID(
@nSiteID INT,
@tStartDate DateTime,
@strServiceIDs VARCHAR(255)--ServiceID组合,中间用逗号隔开的
)
As
Begin
Select ID From ServiceRate Where SiteID = @nSiteID And StartDate = @tStartDate And ', ' + @strServiceIDs + ', ' Like '%, ' + Cast(ServiceID As Varchar) + ',% '
End
GO
------解决方案--------------------
----不知道楼主是哪个意思
CREATE PROCEDURE Pro_GetServiceRateID
@nSiteID INT,
@tStartDate DateTime,
@strServiceIDs VARCHAR(255)--ServiceID组合,中间用逗号隔开的
As
Set NoCount On ---禁止回显消息
Select
Case When SiteID=@nSiteID And StartDate=@tStartDate And
CharIndex( ', '+Cast(ServiceID As Varchar)+ ', ', ', '+@strServiceIDs+ ', ')> 0
Then ID Else 0 End As ID
From
ServiceRate
GO

-------------------------------------
CREATE PROCEDURE Pro_GetServiceRateID
@nSiteID INT,
@tStartDate DateTime,
@strServiceIDs VARCHAR(255)--ServiceID组合,中间用逗号隔开的
As
Set NoCount On ---禁止回显消息
Select
IsNull(ID,0) As ID
From
ServiceRate
Where
SiteID=@nSiteID And StartDate=@tStartDate And
CharIndex( ', '+Cast(ServiceID As Varchar)+ ', ', ', '+@strServiceIDs+ ', ')> 0
GO

---调用存储过程
EXEC Pro_GetServiceRateID 1, '2007-08-25 ', '1,2,4 '