SQL Dynamic Query程序基于不同程序的值

时间:2017-05-18 05:37:13

标签: sql sql-server stored-procedures

我想拥有包含动态查询和给出完美结果的程序。但我想在另一个程序中使用该程序。

当我编写像

这样的查询时
case R.id.restart:
  for (Product p : boxAdapter.getBox()) {
  String result = "Selected Product are :";
   if (p.box){ // if check
    result += "\n" + p.name;
   }
     p.box=false;
  }
  adapter.notifyDataSetChanged();
  return true;
}

它返回

  

过程不是公认的内置函数名称。

3 个答案:

答案 0 :(得分:0)

只需从testb调用testa,如:

EXEC testa @newId, @prod, @desc;

确保使用以下内容获取@id:

SELECT @newId = @@SCOPE_IDENTITY

答案 1 :(得分:0)

你应该从你的程序中复制一个临时表,并使用INSERT..EXEC插入你的第一个proc的结果:

create #t (...);
insert #t(...)  exec sp1;

此时你的第二个过程可以使用#t从中进行选择

另一种解决方案是将你的proc转换为多语句功能,但由于你使用动态代码,情况并非如此

答案 2 :(得分:0)

我有一个名为savedsearch的表,其中有几列名为username mobile lastloginbefore等

然后我将此程序用于动态查询

ALTER PROCEDURE [dbo].[usp_GetCouponStatusProcedure1] --26072,54
@UserID INT,
@SavedSearch VARCHAR(550)

AS 开始     DECLARE @ Email1 VARCHAR(50),             @ Mobile1 VARCHAR(50),             @ UserFilter1 VARCHAR(50),             @ UserName1 VARCHAR(50),             @ UserType1 VARCHAR(50),             @ LastLoginBefore1 VARCHAR(50),             @ LastLogin1 VARCHAR(50),             @ LastPurchase1 VARCHAR(50)

SELECT @UserName1 = ss.UserName,
       @Email1               = ss.EmailID,
       @Mobile1              = ss.Mobile,
       @UserFilter1          = ss.UserFilter,
       @UserType1            = ss.UserType,
       @LastLoginBefore1     = ss.LastLoginBefore,
       @LastPurchase1        = ss.LastPurchase,
       @LastLogin1           = ss.LastLogin
FROM   SavedSearch AS ss
WHERE  ss.ID = @SavedSearch  

DECLARE @String NVARCHAR(1000) =
        'SELECT  SearchName  into #Temp

FROM SavedSearch AS ss'

DECLARE @LastLogin           VARCHAR(500),
        @Email               VARCHAR(500),
        @Mobile              VARCHAR(500),
        @UserFilter          VARCHAR(500),
        @UserName            VARCHAR(500),
        @UserType            VARCHAR(500),
        @LastLoginBefore     VARCHAR(500),
        @LastPurchase        VARCHAR(500),
        @WhereClause         VARCHAR(500),
        @WhereClause1        VARCHAR(500),
        @WhereClause2        VARCHAR(500),
        @WhereClause3        VARCHAR(500),
        @WhereClause4        VARCHAR(500),
        @WhereClause5        VARCHAR(500),
        @WhereClause6        VARCHAR(500),
        @WhereClause7        VARCHAR(500),
        @WhereClause8        VARCHAR(500)  

SET @WhereClause = '        WHERE  ss.ID = @SavedSearch1 AND ('  

DECLARE @i INT   
IF @LastLogin1 <> 0
BEGIN
    SET @LastLogin = 
        '      CROSS APPLY funcToGetNewUserLastLogin(ss.LastLogin) AS ftll  '  

    SET @WhereClause1 = ' ftll.[User_ID] = @UserID1  '
END   

IF @Email1 <> ''
BEGIN
    SET @Email = 
        '      CROSS APPLY funcToGetUserForGivenEmail(ss.EmailID) AS ftEm  '  

    SET @WhereClause2 = ' ftEm.[User_ID] = @UserID1  '
END   

IF @Mobile1 <> 0
BEGIN
    SET @Mobile = 
        '      CROSS APPLY funcToGetUserForGivenMobile(ss.Mobile) AS ftmo  '  

    SET @WhereClause3 = ' ftmo.[User_ID] = @UserID1  '
END   

IF @UserFilter1 <> ''
BEGIN
    SET @UserFilter = 
        '      CROSS APPLY funcToGetUserForGivenUserFilter(ss.UserFilter) AS ftUf  '  

    SET @WhereClause4 = ' ftUf.[User_ID] = @UserID1  '
END   

IF @UserName1 <> ''
BEGIN
    SET @UserName = 
        '      CROSS APPLY funcToGetUserForGivenUserName(ss.UserName) AS ftun  '  

    SET @WhereClause5 = ' ftun.[User_ID] = @UserID1  '
END   

IF @UserType1 <> ''
BEGIN
    SET @UserType = 
        '      CROSS APPLY funcToGetUserForGivenUserType(ss.UserType) AS ftut  '  

    SET @WhereClause6 = ' ftut.[User_ID] = @UserID1  '
END   

IF @LastLoginBefore1 <> 0
BEGIN
    SET @LastLoginBefore = 
        '      CROSS APPLY funcToGetUserLastLoginBefore(ss.LastLoginBefore) AS ftlb  '  

    SET @WhereClause7 = ' ftlb.[User_ID] = @UserID1  '
END   

IF @LastPurchase1 <> 0
BEGIN
    SET @LastPurchase = 
        '      CROSS APPLY funcToGetUserLastPurchased(ss.LastPurchase) AS ftpur  '  

    SET @WhereClause8 = ' ftpur.[User_ID] = @UserID1  '
END   



SET @String = @String + ISNULL(@Email, '') + ISNULL(@UserName, '') + ISNULL(@UserType, '') 
    + ISNULL(@UserFilter, '') + ISNULL(@LastLogin, '') 
    + ISNULL(@LastLoginBefore, '') + ISNULL(@LastPurchase, '') + ISNULL(@Mobile, '') 
    + ISNULL(@WhereClause, '') + CASE 
                                      WHEN @WhereClause1 <> '' THEN CASE 
                                                                         WHEN 
                                                                              @WhereClause2 
                                                                              <
                                                                              >
                                                                              '' 
                                                                              OR 
                                                                              @WhereClause3 
                                                                              <
                                                                              >
                                                                              '' 
                                                                              OR 
                                                                              @WhereClause8 
                                                                              <
                                                                              >
                                                                              '' 
                                                                              OR 
                                                                              @WhereClause7 
                                                                              <
                                                                              >
                                                                              '' 
                                                                              OR 
                                                                              @WhereClause6 
                                                                              <
                                                                              >
                                                                              '' 
                                                                              OR 
                                                                              @WhereClause5 
                                                                              <
                                                                              >
                                                                              '' 
                                                                              OR 
                                                                              @WhereClause4 
                                                                              <
                                                                              >
                                                                              '' THEN 
                                                                              ISNULL(@WhereClause1, '') 
                                                                              +
                                                                              ' OR '
                                                                         ELSE 
                                                                              ISNULL(@WhereClause1, '')
                                                                    END
                                      ELSE ''
                                 END + CASE 
                                            WHEN @WhereClause2 <> '' THEN CASE 
                                                                               WHEN 
                                                                                    @WhereClause3 
                                                                                    <
                                                                                    >
                                                                                    '' 
                                                                                    OR 
                                                                                    @WhereClause8 
                                                                                    <
                                                                                    >
                                                                                    '' 
                                                                                    OR 
                                                                                    @WhereClause7 
                                                                                    <
                                                                                    >
                                                                                    '' 
                                                                                    OR 
                                                                                    @WhereClause6 
                                                                                    <
                                                                                    >
                                                                                    '' 
                                                                                    OR 
                                                                                    @WhereClause5 
                                                                                    <
                                                                                    >
                                                                                    '' 
                                                                                    OR 
                                                                                    @WhereClause4 
                                                                                    <
                                                                                    >
                                                                                    '' THEN 
                                                                                    ISNULL(@WhereClause2, '') 
                                                                                    +
                                                                                    ' OR '
                                                                               ELSE 
                                                                                    ISNULL(@WhereClause2, '')
                                                                          END
                                            ELSE ''
                                       END + CASE 
                                                  WHEN @WhereClause3 <> '' THEN CASE 
                                                                                     WHEN 
                                                                                          @WhereClause8 
                                                                                          <
                                                                                          >
                                                                                          '' 
                                                                                          OR 
                                                                                          @WhereClause7 
                                                                                          <
                                                                                          >
                                                                                          '' 
                                                                                          OR 
                                                                                          @WhereClause6 
                                                                                          <
                                                                                          >
                                                                                          '' 
                                                                                          OR 
                                                                                          @WhereClause5 
                                                                                          <
                                                                                          >
                                                                                          '' 
                                                                                          OR 
                                                                                          @WhereClause4 
                                                                                          <
                                                                                          >
                                                                                          '' THEN 
                                                                                          ISNULL(@WhereClause3, '') 
                                                                                          +
                                                                                          ' OR '
                                                                                     ELSE 
                                                                                          ISNULL(@WhereClause3, '')
                                                                                END
                                                  ELSE ''
                                             END + CASE 
                                                        WHEN @WhereClause4 <
                                                             > '' THEN CASE 
                                                                            WHEN 
                                                                                 @WhereClause8 
                                                                                 <
                                                                                 >
                                                                                 '' 
                                                                                 OR 
                                                                                 @WhereClause7 
                                                                                 <
                                                                                 >
                                                                                 '' 
                                                                                 OR 
                                                                                 @WhereClause6 
                                                                                 <
                                                                                 >
                                                                                 '' 
                                                                                 OR 
                                                                                 @WhereClause5 
                                                                                 <
                                                                                 >
                                                                                 '' THEN 
                                                                                 ISNULL(@WhereClause4, '') 
                                                                                 +
                                                                                 ' OR '
                                                                            ELSE 
                                                                                 ISNULL(@WhereClause4, '')
                                                                       END
                                                        ELSE ''
                                                   END + CASE 
                                                              WHEN @WhereClause5 
                                                                   <> '' THEN CASE 
                                                                                   WHEN 
                                                                                        @WhereClause8 
                                                                                        <
                                                                                        >
                                                                                        '' 
                                                                                        OR 
                                                                                        @WhereClause7 
                                                                                        <
                                                                                        >
                                                                                        '' 
                                                                                        OR 
                                                                                        @WhereClause6 
                                                                                        <
                                                                                        >
                                                                                        '' THEN 
                                                                                        ISNULL(@WhereClause5, '') 
                                                                                        +
                                                                                        ' OR '
                                                                                   ELSE 
                                                                                        ISNULL(@WhereClause5, '')
                                                                              END
                                                              ELSE ''
                                                         END + CASE 
                                                                    WHEN @WhereClause6 
                                                                         <>
                                                                         '' THEN CASE 
                                                                                      WHEN 
                                                                                           @WhereClause8 
                                                                                           <
                                                                                           >
                                                                                           '' 
                                                                                           OR 
                                                                                           @WhereClause7 
                                                                                           <
                                                                                           >
                                                                                           '' THEN 
                                                                                           ISNULL(@WhereClause6, '') 
                                                                                           +
                                                                                           ' OR '
                                                                                      ELSE 
                                                                                           ISNULL(@WhereClause6, '')
                                                                                 END
                                                                    ELSE ''
                                                               END + CASE 
                                                                          WHEN 
                                                                               @WhereClause7 
                                                                               <
                                                                               >
                                                                               '' THEN CASE 
                                                                                            WHEN 
                                                                                                 @WhereClause8 
                                                                                                 <
                                                                                                 >
                                                                                                 '' THEN 
                                                                                                 ISNULL(@WhereClause7, '') 
                                                                                                 +
                                                                                                 ' OR '
                                                                                            ELSE 
                                                                                                 ISNULL(@WhereClause7, '')
                                                                                       END
                                                                          ELSE 
                                                                               ''
                                                                     END + CASE 
                                                                                WHEN 
                                                                                     @WhereClause6 
                                                                                     <
                                                                                     >
                                                                                     '' THEN 
                                                                                     ISNULL(@WhereClause6, '')
                                                                                ELSE 
                                                                                     ''
                                                                           END 
    + ' )'  


DECLARE @ParmDefinition NVARCHAR(500) =
        N'@UserID1 INT, @SavedSearch1 varchar(500),@ii int OUTPUT';    
DECLARE @UserID1          INT,
        @SavedSearch1     VARCHAR(500) 

EXECUTE sp_executesql @String,
     @ParmDefinition,
     @UserID1 = @UserID,
     @SavedSearch1 = @SavedSearch,
     @ii = @i OUTPUT

DECLARE @rowcount INT
SET @rowcount = @@rowcount
SELECT --@SavedSearch  AS savedsearchid,
       @rowcount AS ReturnedValue

       --PRINT @String

       --SELECT @i

END