传递长查询时出错

本文关键字:出错 查询 | 更新日期: 2023-09-27 17:50:09

我有一个查询。

DECLARE  @fromDate AS DATETIME = '01/28/2009';
DECLARE  @ToDate AS DATETIME = '01/28/2011';
DECLARE  @IsMonthWise AS BIT = 1
DECLARE  @temp AS TABLE(   userId BIGINT
                         , Visits INT
                         , DownLoads INT )
DECLARE  @tempmonth AS TABLE(   userId BIGINT
                              , Visits INT
                              , DownLoads INT
                              , MonthYear NVARCHAR( 50 ) )
IF @IsMonthWise = 0
BEGIN
    INSERT INTO @temp(   UserId
                       , Visits
                       , DownLoads )
    SELECT  Main.user_id             AS UserId
          , COUNT( Main.user_id )    AS Visits
          , ( SELECT  COUNT( user_id )
              FROM  content_hits_tbl AS Sub
              WHERE  content_id <> 0 AND
                     Main.user_id = Sub.user_id AND
                     visit_type = 2 AND
                     CAST( CONVERT( VARCHAR, hit_date, 101 ) AS DATETIME ) BETWEEN CAST( CONVERT( VARCHAR, @fromDate, 101 ) AS DATETIME ) AND CAST( CONVERT( VARCHAR, @ToDate, 101 ) AS DATETIME )
              GROUP BY  USER_ID )
    FROM  content_hits_tbl AS Main
    INNER JOIN users
    ON  Main.user_id = users.user_id
    WHERE  content_id = 0 AND
           visit_type = 1 AND
           users.user_deleted = 0 AND
           CAST( CONVERT( VARCHAR, hit_date, 101 ) AS DATETIME ) BETWEEN CAST( CONVERT( VARCHAR, @fromDate, 101 ) AS DATETIME ) AND CAST( CONVERT( VARCHAR, @ToDate, 101 ) AS DATETIME )
    GROUP BY  Main.user_id
    SELECT  Users.last_name + ',' + Users.first_name AS User_Name
          , REPLACE( REPLACE( REPLACE( REPLACE( region.value_string, '1', 'APR' ), '2', 'LAR' ), '3', 'EAMER' ), '4', 'US&C' ) AS User_Region
          , CASE
                WHEN Users.membership_user = 0 THEN 'Internal'
                ELSE 'External'
            END AS User_Type
          , Visits                   AS No_of_Visits
          , ISNULL( DownLoads, 0 )   AS No_of_DownLoads
          , Users.user_id            AS UserId
          , ( SELECT  userData.value_string
              FROM  dynamic_data_tbl AS userData
              WHERE  userData.object_id = user_id AND
                     definition_id = 2147483649 ) AS [Organizational Group]
    FROM  users AS Users
    INNER JOIN @temp AS temp
    ON  temp.UserId = Users.user_id
    INNER JOIN dynamic_data_tbl AS region
    ON  region.object_id = Users.user_id
    WHERE  region.definition_id = 43
    ORDER BY  No_of_Visits DESC
END
ELSE
BEGIN
    INSERT INTO @tempmonth(   UserId
                            , Visits
                            , DownLoads
                            , MonthYear )
    SELECT  Main.user_id             AS UserId
          , COUNT( Main.user_id )    AS Visits
          , ( SELECT  COUNT( user_id )
              FROM  content_hits_tbl AS Sub
              WHERE  content_id <> 0 AND
                     Main.user_id = Sub.user_id AND
                     DATENAME( mm, Main.hit_date ) + '-' + DATENAME( yyyy, Main.hit_date ) = DATENAME( mm, Sub.hit_date ) + '-' + DATENAME( yyyy, Sub.hit_date ) AND
                     visit_type = 2 AND
                     CAST( CONVERT( VARCHAR, hit_date, 101 ) AS DATETIME ) BETWEEN CAST( CONVERT( VARCHAR, @fromDate, 101 ) AS DATETIME ) AND CAST( CONVERT( VARCHAR, @ToDate, 101 ) AS DATETIME )
              GROUP BY  USER_ID
                      , DATENAME( mm, hit_date ) + '-' + DATENAME( yyyy, hit_date ) )
          , DATENAME( mm, hit_date ) + '-' + DATENAME( yyyy, hit_date ) AS MonthYear
    FROM  content_hits_tbl AS Main
    INNER JOIN users
    ON  Main.user_id = users.user_id
    WHERE  content_id = 0 AND
           visit_type = 1 AND
           users.user_deleted = 0 AND
           CAST( CONVERT( VARCHAR, hit_date, 101 ) AS DATETIME ) BETWEEN CAST( CONVERT( VARCHAR, @fromDate, 101 ) AS DATETIME ) AND CAST( CONVERT( VARCHAR, @ToDate, 101 ) AS DATETIME )
    GROUP BY  Main.user_id
            , DATENAME( mm, hit_date ) + '-' + DATENAME( yyyy, hit_date )
    SELECT  Users.last_name + ',' + Users.first_name AS User_Name
          , REPLACE( REPLACE( REPLACE( REPLACE( region.value_string, '1', 'APR' ), '2', 'LAR' ), '3', 'EAMER' ), '4', 'US&C' ) AS User_Region
          , CASE
                WHEN Users.membership_user = 0 THEN 'Internal'
                ELSE 'External'
            END AS User_Type
          , Visits                   AS No_of_Visits
          , ISNULL( DownLoads, 0 )   AS No_of_DownLoads
          , Users.user_id            AS UserId
          , temp.MonthYear           AS MonthYear
          , ( SELECT  userData.value_string
              FROM  dynamic_data_tbl AS userData
              WHERE  userData.object_id = user_id AND
                     definition_id = 2147483649 ) AS [Organizational Group]
    FROM  users AS Users
    INNER JOIN @tempmonth AS temp
    ON  temp.UserId = Users.user_id
    INNER JOIN dynamic_data_tbl AS region
    ON  region.object_id = Users.user_id
    WHERE  region.definition_id = 43
    ORDER BY  User_Name ASC
            , MonthYear DESC
END
DELETE FROM @temp
DELETE FROM @tempmonth

IT工作良好,如果在SQL服务器运行。但如果我传递相同的字符串从c#代码(ExecuteReader)。未执行

如果我从代码后面复制查询并粘贴在sql server中,它将在单行中出现。我想这就是为什么我的查询没有执行的原因。

请让我知道如何从我的应用程序(c#)传递执行长查询的方式

谢谢

传递长查询时出错

尝试先声明变量:

DECLARE  @fromDate AS DATETIME;
DECLARE  @ToDate AS DATETIME;

等等……

,然后设置它们

SET @fromDate = '01/28/2009';
SET @ToDate = '01/28/2011';

不确定这是否会解决您的问题,或者如果没有看到设置SqlCommand对象并执行此查询的代码就不难判断,但这是为变量赋值的首选方法