如何用多个变量编写动态sql

时间:2014-02-11 12:41:39

标签: sql-server

我有一张顾客的桌子, 我想建立功能或sp,让他们的城市回归客户, 它应该允许输入多个变量。 我该怎么办?

3 个答案:

答案 0 :(得分:0)

我认为你的意思是存储过程必须接受一些参数。

您可以通过转到SSMS中的数据库,可编程性,然后右键单击“存储过程”并选择“新存储过程”来完成此操作。它将为您生成一个模板,您可以在其中看到它的形式:

CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> 
    -- Add the parameters for the stored procedure here
    <@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
    <@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS

Ex: CREATE PROCEDURE MYPROC @PARAM1 INT, @PARAM2 NVARCHAR(MAX) AS ... 

我希望它有所帮助,

罗杰

答案 1 :(得分:0)

This可能仅对准备动态查询很有帮助。如果要为特定参数传递N个值,则应使用UserDefinedType(甚至是UserDefinedTable类型)。

使用可存储为参数的存储过程(结构化数据类型)

1.CREATE CUSTOM STRUCTURE TYPE INDATABASE
    EX.
    CREATE TYPE [CUSTOM_DATATYPE_NAME] AS TABLE (FIELD_NAME DATATYPE,...)
2.CREATE STORED PROCEDURE WITH CUSTOM PARAMS 
    EX.
    CREATE PROCEDURE [PROCEDURE_NAME] (@[VARIABLE_NAME] CUSTOM_DATATYPE_NAME READONLY,...)
3.USE ABOVE CUSTOM_DATATYPE_NAME DIRECTLY IN INSERT, UPDATE STATEMENT
4.IN CS FILE (IN CODE TO CALL THIS PROCEDURE AND PASS DATA TABLE)

    SqlCommand sqCom = new SqlCommand();
        sqCom.CommandText = "[PROCEDURE_NAME]";
        sqCom.CommandType = CommandType.StoredProcedure;

        sqCom.Parameters.Add("@[VARIABLE_NAME]", SqlDbType.Structured);
        sqCom.Parameters.Add("@Notice", SqlDbType.VarChar, 300);
        sqCom.Parameters.Add("@InsertedBy", SqlDbType.Int);

        sqCom.Parameters[0].Value = [DATA_TABLE];
        sqCom.Parameters[1].Value = Notice;
        sqCom.Parameters[2].Value = userid;
        sqCom.Connection = conSQL;
        sqCom.Connection.Open();

        int affectedRows=sqCom.ExecuteNonQuery();
        sqCom.Connection.Close();
        return affectedRows;

答案 2 :(得分:0)

我创建了一个存储过程,允许一个参数(@inSelectFilter),用户可以在其中输入select语句

然后,在SP中,我将其拆分为表变量并将其构建为完整的WHERE语句,以动态传递给select语句

--BUILD THE INDIVIDUAL SELECT STATEMENTS INTO A TEMP TABLE
    SET @RowPosition = 1;
    SET @inSelectFilter = @inSelectFilter + ';';
    SET @inSelectFilter = REPLACE(@inSelectFilter,'/','''');
    WHILE LEN(@inSelectFilter) > 0
    BEGIN
        IF PATINDEX('%;%',@inSelectFilter) > 0
        BEGIN
            SET @SelectParameter = SUBSTRING(@inSelectFilter, 0, PATINDEX('%;%',@inSelectFilter))
            SET @inSelectFilter = SUBSTRING(@inSelectFilter, LEN(@SelectParameter + ';') + 1,LEN(@inSelectFilter))
            IF @RowPosition = 1
                BEGIN
                INSERT INTO @SelectParameterTable VALUES ('WHERE ' + @SelectParameter)
                END
            ELSE
                BEGIN
                INSERT INTO @SelectParameterTable VALUES (' AND ' + @SelectParameter)
                END
            SET @RowPosition = @RowPosition + 1        
        END
        ELSE
        BEGIN
            SET @SelectParameter = @inSelectFilter
            SET @inSelectFilter = NULL
        END
    END

    --BUILD THE COMPLETE WHERE STATEMENT
    SET @SelectParameter = NULL;
    SELECT @SelectParameter = COALESCE(@SelectParameter, '') + statementString FROM @SelectParameterTable 
    SET @SelectParameterTemp = @SelectParameter;
    Print 'Selection statement: ' + @SelectParameter

唯一要记住的是使用某种转义字符而不是'以便参数可以传递

所以SP是

ALTER PROCEDURE [usp_FindMembers] (@inSelectFilter NVARCHAR(1000))

并像这样执行...

exec usp_FindMembers 'STATUS=/A/ OR (STATUS IN (/N/,/PC/,/PD/,/PE/,/PN/) AND PAYGROUP IN (/X1/,/C1/,/X3/,/T2/))'