转换动态存储过程

发布于 2024-10-22 01:30:36 字数 3801 浏览 0 评论 0原文

我想知道是否有办法使这个动态 sp 成为非动态 sp

CREATE PROCEDURE [dbo].[GetPeople]
    (
        @id varchar(8) = NULL,
        @lastName varchar(50) = NULL,
        @firstName varchar(50) = NULL,
        @birthdate date = NULL,
        @ssn varchar(9) = NULL,
        @driversLicense varchar(12) = NULL,
        @gender varchar(1) = NULL,

        @addressNumber varchar(8) = NULL,
        @addressFraction varchar(3) = NULL,
        @addressDirection varchar(3) = NULL,
        @street varchar(45) = NULL,
        @addressSuffix varchar(4) = NULL,
        @addressPostDirection varchar(2) = NULL,
        @addressUnitType varchar(6) = NULL,
        @addressUnit varchar(8) = NULL,
        @city varchar(22) = NULL,
        @zip varchar(5) = NULL,
        @addressStatus varchar(1) = NULL

    )
AS
    SET NOCOUNT ON

    DECLARE @q nvarchar(4000),
            @paramlist  nvarchar(4000)  

    SELECT @q = ' SELECT * FROM People WHERE 1 = 1' 

    IF @certnum <> '' 
        SELECT @q = @q + ' AND ID = ''' + @id  + ''''
    ELSE 
    BEGIN
        IF @lastName  <> ''                                           
            SELECT @q = @q + ' AND lastname like ''' + @lastName + '%'''    
        IF @firstName  <> ''                                           
            SELECT @q = @q + ' AND firstname like ''' + @firstName + '%'''  
        IF @birthdate  is not null                                 
            SELECT @q = @q + ' AND birthdate = ''' + cast(@birthdate as varchar(10)) + ''''
        IF @ssn  <>  ''                                  
            SELECT @q = @q + ' AND ssnum = ''' + @ssn + ''''    
        IF @driversLicense  <>  ''                                  
            SELECT @q = @q + ' AND drivers_license = ''' + @driversLicense + ''''    
        IF @gender  <>  ''                                  
            SELECT @q = @q + ' AND sex = ''' + @gender + '''' 
        IF @street  <> ''                                           
            SELECT @q = @q + ' AND addr_str like ''' + @street + '%'''
        IF @city  <> ''                                           
            SELECT @q = @q + ' AND cityname like ''' + @city + '%'''    
        IF @zip  <> ''                                           
            SELECT @q = @q + ' AND addr_zip like ''' + @zip + '%'''  
        IF @addressNumber  <> ''                                           
            SELECT @q = @q + ' AND ltrim(rtrim(addr_num)) = ''' + @addressNumber + ''''   

    END


    SELECT @q = @q + ' ORDER BY  lastname, firstname'


    SELECT @paramlist = '
        @id varchar(8) = NULL,
        @lastName varchar(50) = NULL,
        @firstName varchar(50) = NULL,
        @birthdate date = NULL,
        @ssn varchar(9) = NULL,
        @driversLicense varchar(12) = NULL,
        @gender varchar(1) = NULL,

        @addressNumber varchar(8) = NULL,
        @addressFraction varchar(3) = NULL,
        @addressDirection varchar(3) = NULL,
        @street varchar(45) = NULL,
        @addressSuffix varchar(4) = NULL,
        @addressPostDirection varchar(2) = NULL,
        @addressUnitType varchar(6) = NULL,
        @addressUnit varchar(8) = NULL,
        @city varchar(22) = NULL,
        @zip varchar(5) = NULL,
        @addressStatus varchar(1) = NULL
'

    PRINT @q
    EXEC sp_executesql @q, @paramlist, 
        @id,

        @lastName,
        @firstName,
        @birthdate,
        @ssn,
        @driversLicense,
        @gender,

        @addressNumber,
        @addressFraction,
        @addressDirection,
        @street,
        @addressSuffix,
        @addressPostDirection,
        @addressUnitType,
        @addressUnit,
        @city,
        @zip,
        @addressStatus

感谢您的帮助

I was wondering if there was a way to make this dynamic sp to a non-dynamic sp

CREATE PROCEDURE [dbo].[GetPeople]
    (
        @id varchar(8) = NULL,
        @lastName varchar(50) = NULL,
        @firstName varchar(50) = NULL,
        @birthdate date = NULL,
        @ssn varchar(9) = NULL,
        @driversLicense varchar(12) = NULL,
        @gender varchar(1) = NULL,

        @addressNumber varchar(8) = NULL,
        @addressFraction varchar(3) = NULL,
        @addressDirection varchar(3) = NULL,
        @street varchar(45) = NULL,
        @addressSuffix varchar(4) = NULL,
        @addressPostDirection varchar(2) = NULL,
        @addressUnitType varchar(6) = NULL,
        @addressUnit varchar(8) = NULL,
        @city varchar(22) = NULL,
        @zip varchar(5) = NULL,
        @addressStatus varchar(1) = NULL

    )
AS
    SET NOCOUNT ON

    DECLARE @q nvarchar(4000),
            @paramlist  nvarchar(4000)  

    SELECT @q = ' SELECT * FROM People WHERE 1 = 1' 

    IF @certnum <> '' 
        SELECT @q = @q + ' AND ID = ''' + @id  + ''''
    ELSE 
    BEGIN
        IF @lastName  <> ''                                           
            SELECT @q = @q + ' AND lastname like ''' + @lastName + '%'''    
        IF @firstName  <> ''                                           
            SELECT @q = @q + ' AND firstname like ''' + @firstName + '%'''  
        IF @birthdate  is not null                                 
            SELECT @q = @q + ' AND birthdate = ''' + cast(@birthdate as varchar(10)) + ''''
        IF @ssn  <>  ''                                  
            SELECT @q = @q + ' AND ssnum = ''' + @ssn + ''''    
        IF @driversLicense  <>  ''                                  
            SELECT @q = @q + ' AND drivers_license = ''' + @driversLicense + ''''    
        IF @gender  <>  ''                                  
            SELECT @q = @q + ' AND sex = ''' + @gender + '''' 
        IF @street  <> ''                                           
            SELECT @q = @q + ' AND addr_str like ''' + @street + '%'''
        IF @city  <> ''                                           
            SELECT @q = @q + ' AND cityname like ''' + @city + '%'''    
        IF @zip  <> ''                                           
            SELECT @q = @q + ' AND addr_zip like ''' + @zip + '%'''  
        IF @addressNumber  <> ''                                           
            SELECT @q = @q + ' AND ltrim(rtrim(addr_num)) = ''' + @addressNumber + ''''   

    END


    SELECT @q = @q + ' ORDER BY  lastname, firstname'


    SELECT @paramlist = '
        @id varchar(8) = NULL,
        @lastName varchar(50) = NULL,
        @firstName varchar(50) = NULL,
        @birthdate date = NULL,
        @ssn varchar(9) = NULL,
        @driversLicense varchar(12) = NULL,
        @gender varchar(1) = NULL,

        @addressNumber varchar(8) = NULL,
        @addressFraction varchar(3) = NULL,
        @addressDirection varchar(3) = NULL,
        @street varchar(45) = NULL,
        @addressSuffix varchar(4) = NULL,
        @addressPostDirection varchar(2) = NULL,
        @addressUnitType varchar(6) = NULL,
        @addressUnit varchar(8) = NULL,
        @city varchar(22) = NULL,
        @zip varchar(5) = NULL,
        @addressStatus varchar(1) = NULL
'

    PRINT @q
    EXEC sp_executesql @q, @paramlist, 
        @id,

        @lastName,
        @firstName,
        @birthdate,
        @ssn,
        @driversLicense,
        @gender,

        @addressNumber,
        @addressFraction,
        @addressDirection,
        @street,
        @addressSuffix,
        @addressPostDirection,
        @addressUnitType,
        @addressUnit,
        @city,
        @zip,
        @addressStatus

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失去的东西太少 2024-10-29 01:30:37

您可以对其进行转换,但您的代码效率会低于您所拥有的代码。你为什么要这么做?这种类型的搜索过程是动态 SQL 是最佳方法的少数几个地方之一。

YOu can convert it but you would have code that is less efficient than what you have. Why would you do that? This type of search proc is one of the few places where dynamic SQL is the best method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文