我有一个存储过程。需要传入一个 查询数据源的字符串,然后在存储中使用EXEC执行这个字符串,将得到的数据源插入一张临时表。下面是存储过程刚开始的代码:CREATE PROCEDURE GLBS_Processor         @DataSourceQuery  varchar(100)  
--[ID] int   OUTPUT
AS
        --创建用于存储异常数据的临时表: 
       CREATE TABLE #retTable( primaryID int IDENTITY (1,1) PRIMARY KEY ,ID int not null ,value decimal(15,8))         --创建用于存储分析数据的临时表: 
       CREATE TABLE #tempTable( primaryID int IDENTITY (1,1) PRIMARY KEY ,ID int not null ,value decimal(15,8)) 
     
       --将需要处理的数据插入临时表
        insert into #tempTable (ID,value) exec(@DataSourceQuery)如果 (C#语言)传入一个sqlcommand参数:“select ID,XD from AccountConcrete Where ID in (Select ID from TrialFiles where ProjectId=1 ) ”;
 执行没有错误。但是当我传入:“select ID,XD from AccountConcrete Where ID in (Select ID from TrialFiles where ProjectId=1 and AccountTypeNum='A02N030001') and TrialDate>= '2009-12-20' and TrialDate<= '2010-1-20'”时,就会报 语法错误,单独在查询分析器中执行正常。请问这种情况如何解决?
 如果想在存储外部将WHERE条件中的值赋好,而不用用变量传进来再构造字符串,该怎么做?谢谢各位。

解决方案 »

  1.   

    --1. 构造使用IN子句的动态Transact-SQL方法进行编号查询--a. 要查询的字段类型是数字型--查询的值列表
    DECLARE @idlist varchar(100)
    SET @idlist='1,2,3'--拼接并执行动态Transact-SQL语句
    EXEC('SELECT * FROM tbname WHERE fdname IN('+@idlist+')')
    GO--b. 要查询的字段类型是字符型
    --查询的值列表已经加上了字符串边界符
    DECLARE @idlist varchar(100)
    SET @idlist='''a'',''b''''a'',''c'''--拼接并执行动态Transact-SQL语句
    EXEC('SELECT * FROM tbname WHERE fdname IN('+@idlist+')')
    GO--查询的值列表没有字符串边界符
    DECLARE @idlist varchar(100)
    SET @idlist='a,b''a,c'--由于是字段类型是,所以在拼接时,必须为其加上字符串边界符(')
    DECLARE @s varchar(1000)
    SET @s=''''
        +REPLACE(REPLACE(@idlist,'''',''''''),',',''',''')
        +''''--拼接并执行动态Transact-SQL语句
    EXEC('SELECT * FROM tbname WHERE fdname IN('+@s+')')
    GO/*=====================================================*/
    --2. 使用LIKE或者PATINDEX进行编号查询
    --查询的值列表
    DECLARE @idlist varchar(100)
    SET @idlist='1,2,3'--查询
    SELECT * FROM tbname WHERE CHARINDEX(','+RTRIM(fdname)+',',','+@idlist+',')>0
    SELECT * FROM tbname WHERE PATINDEX('%,'+RTRIM(fdname)+',%',','+@idlist+',')>0
    SELECT * FROM tbname WHERE ','+@idlist+',' LIKE '%,'+RTRIM(fdname)+',%'
    GO/*=====================================================*/
    --3. 编号查询中常见的错误
    --a. 最容易犯的错误:表达式充当表达式列表。
    DECLARE @s varchar(100)
    SET @s='1'
    SELECT id,name FROM sysobjects WHERE id IN(@s)
    /*--结果
    id          name 
    ---------------- ------------
    1           sysobjects
    --*/SET @s='1,2,3'
    SELECT id,name FROM sysobjects WHERE id IN(@s)
    /*--结果
    服务器: 消息 245,级别 16,状态 1,行 3
    将 varchar 值 '1,2,3' 转换为数据类型为 int 的列时发生语法错误。
    --*/
    GO--b. 生成动态Transact-SQL语句时忽略了数据类型。
    DECLARE @s varchar(100)
    SET @s='U,S'
    EXEC('SELECT id,name FROM sysobjects WHERE id IN('+@s+')')
    /*--结果:
    服务器: 消息 207,级别 16,状态 3,行 1
    列名 'S' 无效。
    服务器: 消息 207,级别 16,状态 1,行 1
    列名 'U' 无效。
    --*/
    GO--c. 忽略了比较的精确性问题。
    --要查询的数据
    DECLARE @t TABLE(col varchar(10))
    INSERT @t SELECT '1'
    UNION ALL SELECT '11'
    UNION ALL SELECT '111'
    UNION ALL SELECT '22'--查询
    DECLARE @s varchar(100)
    SET @s='111,22'
    SELECT * FROM @t WHERE CHARINDEX(col,@s)>0
    /*--结果
    col        
    ---------- 
    1
    11
    111
    22
    -*/
    GO
      

  2.   

           --将需要处理的数据插入临时表
            insert into #tempTable (ID,value) exec(@DataSourceQuery)
    LZ
    这句前,print @DataSourceQuery 出来看看是否是select ID,XD from AccountConcrete Where ID in (Select ID from TrialFiles where ProjectId=1 and AccountTypeNum='A02N030001') and TrialDate>= '2009-12-20' and TrialDate <= '2010-1-20'怀疑可能是截取了。 
    看定义的时候好像是varchar(100),100是否够用啊???
      

  3.   

    [code=SQL]select ID,XD from AccountConcrete Where ID in (Select ID from TrialFiles where ProjectId=1 and AccountTypeNum=''A02N030001') and TrialDate>= '2009-12-20' and TrialDate <= '2010-1-20'带单引号的是两个表一个PRINT ('select ID,XD
     from AccountConcrete 
    Where ID in (Select ID from TrialFiles where 
    ProjectId=1 and AccountTypeNum=''A02N030001'')
     and TrialDate>= ''2009-12-20'' 
    and TrialDate <= ''2010-1-20''')select ID,XD
     from AccountConcrete 
    Where ID in (Select ID from TrialFiles where 
    ProjectId=1 and AccountTypeNum='A02N030001')
     and TrialDate>= '2009-12-20' 
    and TrialDate <= '2010-1-20'[/code]
      

  4.   

    PRINT ('select ID,XD 
    from AccountConcrete 
    Where ID in (Select ID from TrialFiles where 
    ProjectId=1 and AccountTypeNum=''A02N030001'') 
    and TrialDate>= ''2009-12-20'' 
    and TrialDate <= ''2010-1-20''') select ID,XD 
    from AccountConcrete 
    Where ID in (Select ID from TrialFiles where 
    ProjectId=1 and AccountTypeNum='A02N030001') 
    and TrialDate>= '2009-12-20' 
    and TrialDate <= '2010-1-20'
      

  5.   

     @DataSourceQuery  varchar(100)  呵呵,100是有点小05用VARCHAR(MAX)
    00还是用VARCHAR(8000)
      

  6.   

    @DataSourceQuery  varchar(100)  100小了点“select ID,XD from AccountConcrete Where ID in (Select ID from TrialFiles where ProjectId=1 and AccountTypeNum='A02N030001') and TrialDate>= '2009-12-20' and TrialDate <= '2010-1-20'”超过100个了