GetInventTable组装SQL语句使用通配符

USE [DEV]
GO
/****** Object:  StoredProcedure [dbo].[GetInventTable]    Script Date: 12/22/2011 14:51:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[GetInventTable]
        @itemId varchar(500) = '',
        @ItemName varchar(500) = '',
        @CNItemName varchar(500) =''
as
begin
    declare @isWhere bit;
    declare @strSQL varchar(256);
    set @isWhere = 0;
    set @strSQL = 'select * from inventTable ';
    if (@itemId ='') and (@ItemName ='') and (@CNItemName ='')
    begin
        exec (@StrSQL);
        return;
    end
    if not(@itemId ='')
    begin
        exec NJ_AddWhere  @strSQL out,@isWhere out;
        set @strSQL = @strSQL + 'itemid ' + dbo.FormatSQL(@itemId) ;
        --exec (@strSQL);
        --return;
    end
    if not(@ItemName ='')
    begin
        exec NJ_AddWhere  @strSQL out,@isWhere out;
        set @strSQL = @strSQL + 'ItemName ' + dbo.FormatSQL(@ItemName) ;
        --exec (@strSQL);
        --return;
    end
    if not(@CNItemName ='')
    begin
        exec NJ_AddWhere @strSQL out,@isWhere out;
        set @strSQL = @strSQL + 'CNItemName' + dbo.FormatSQL(@CNItemName);
        --exec (@strSQL);
        --return;
    end
    exec (@strSQL)
end

posted @ 2011-12-22 14:55  perock  阅读(276)  评论(0编辑  收藏  举报