动态SQL,变量为字符串类型的,单引号用双号表示.

解决方案 »

  1.   

    动态sql语句基本语法 
    1 :普通SQL语句可以用Exec执行 eg:   Select * from tableName 
             Exec('select * from tableName') 
             Exec sp_executesql N'select * from tableName'    -- 请注意字符串前一定要加N 2:字段名,表名,数据库名之类作为变量时,必须用动态SQL eg:   
    declare @fname varchar(20) 
    set @fname = 'FiledName' 
    Select @fname from tableName              -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。 
    Exec('select ' + @fname + ' from tableName')     -- 请注意 加号前后的 单引号的边上加空格 当然将字符串改成变量的形式也可 
    declare @fname varchar(20) 
    set @fname = 'FiledName' --设置字段名 declare @s varchar(1000) 
    set @s = 'select ' + @fname + ' from tableName' 
    Exec(@s)                -- 成功 
    exec sp_executesql @s   -- 此句会报错 declare @s Nvarchar(1000)  -- 注意此处改为nvarchar(1000) 
    set @s = 'select ' + @fname + ' from tableName' 
    Exec(@s)                -- 成功     
    exec sp_executesql @s   -- 此句正确 3. 输出参数 
    declare @num int, 
            @sqls nvarchar(4000) 
    set @sqls='select count(*) from tableName' 
    exec(@sqls) 
    --如何将exec执行结果放入变量中? declare @num int, 
                   @sqls nvarchar(4000) 
    set @sqls='select @a=count(*) from tableName ' 
    exec sp_executesql @sqls,N'@a int output',@num output 
    select @num 
      

  2.   

    这是关健字传参时加上,或在字符串上加
    set   @sql   =   'INSERT   INTO         '           +                                 @inserttable         +         '       ( ['   +       @insertstring     +   ']   , '     +   @insertstring1   +   '           ,           '   +@insertstring2+     '           ,             '   +@insertstring3       +     ')         values             ( '   +     @countnumber   +     '   ,     '   +     @insert_string1   + '               ,           '     +@insert_string2     +         '           ,             '     +@insert_string3     +       ') ' 
      

  3.   

    先谢谢 2楼的朋友:
    在TS里面-->23行的内容是:
    set   @sql   =   'INSERT   INTO         '           +                                 @inserttable         +         '       ( '   +       @insertstring     +   '   , '     +   @insertstring1   +   '           ,           '   +@insertstring2+     '           ,             '   +@insertstring3       +     ')         values             ( '   +     @countnumber   +     '   ,     '   +     @insert_string1   + '               ,           '     +@insert_string2     +         '           ,             '     +@insert_string3     +       ') ' 
    -------------------------------------------------------------------------------------------
    难道
    select   @countnumber   =   count(*)       from     HouseNewTable 
    是非法的,@countnumber不是bigint????
    -------------------------------------------------------------------------------------------
    我改了下23行的插入语句exec  ('INSERT INTO    '     +                @inserttable    +    '   (' +   @insertstring  + ' ,'  + @insertstring1 + '     ,     ' +@insertstring2+  '     ,      ' +@insertstring3   +  '  ,   '   +  @insertstring4   +   '    ,   '   +   @insertstring5   + ')    values      (' +  @countnumber +  ' ,  ' +  @insert_string1 +'       ,     '  +@insert_string2  +    '     ,      '  +@insert_string3  +  '  ,   '   + @insert_string4  +'   ,    ' + @insert_string5   +  ')')---------------------------------------------------------------------------------------------
    执行:
    exec insert_new @inserttable='HouseNewAssisPicTable',@insertstring='NewId',@insertstring1='NewPicturePath',@insertstring2='NewPicturePath1',@insertstring3='NewPicturePath2',@insertstring4='NewPicturePath3',@insertstring5='NewPicturePath4',@insert_string='32152',@insert_string1='"asdes1"',@insert_string2="21adfsad",@insert_string3="dki12",@insert_string4="qreopiu12",@insert_string5="asd98374"
    ---------------------------------------------------------------------------------------------
    执行错误:
    服务器: 消息 128,级别 15,状态 1,行 1
    在此上下文中不允许使用 'asdes1'。此处只允许使用常量、表达式或变量。不允许使用列名。
    ---------------------------------------------------------------------------------------------
    这是怎么回事?
    是不是我对2楼的东西还不够理解深刻?
      

  4.   

    上面方法我都改了试了一下,还是都不行啊!!
    我把关键的地方再贴一遍其它地方我都没有动不贴了:
    --------------------------------TS--->所指出错的23行----------------------------------
    set @sql = 'INSERT INTO    '     +                @inserttable    +    '               (   [ '       +               @insertstring           +       ']    '   + ' ,'  + @insertstring1 + '     ,     ' +@insertstring2+  '     ,      ' +@insertstring3   +  '  ,   '   +  @insertstring4   +   '    ,   '   +   @insertstring5   + ')    values      (' +  @countnumber +  ' ,  ' +  @insert_string1 +'       ,     '  +@insert_string2  +    '     ,      '  +@insert_string3  +  '  ,   '   + @insert_string4  +'   ,    ' + @insert_string5   +  ')'

    ---------------------------------------------------------------------------------------
    执行内容:
    --------------------------------------------------------------------------------------
    exec insert_new @inserttable='HouseNewAssisPicTable',@insertstring='NewId',@insertstring1='NewPicturePath',@insertstring2='NewPicturePath1',@insertstring3='NewPicturePath2',@insertstring4='NewPicturePath3',@insertstring5='NewPicturePath4',@insert_string='32152',@insert_string1='asdes1',@insert_string2='21adfsad',@insert_string3='dki12',@insert_string4='qreopiu12',@insert_string5='asd98374'---------------------------------------------------------------------------------------
    系统报错:
    ------------------------------------------------
    服务器: 消息 8114,级别 16,状态 5,过程 insert_new,行 23
    将数据类型 varchar 转换为 bigint 时出错。
    ------------------------------------------------------
    ????????????????????
      

  5.   

    @insertstring= 'NewId ' 这个NewId 是INT型的吗?
      

  6.   

    是BIGINT
    我在一楼写出了表结构了
    帮忙解决一下,上午马上要结束了
    要快交不了工!!!!!!!
      

  7.   

    你传值传的不对,你把‘NEWID’传给了@insertnumber,而‘NEWID’它仅仅是个字符串,并不是INT型数据
      

  8.   

    按你的意思,传值应该怎么传呢?给点意见好不?
    exec   insert_new   @inserttable= 'HouseNewAssisPicTable ',@insertstring= 'NewId ',@insertstring1= 'NewPicturePath ',@insertstring2= 'NewPicturePath1 ',@insertstring3= 'NewPicturePath2 ',@insertstring4= 'NewPicturePath3 ',@insertstring5= 'NewPicturePath4 ',@insert_string= '32152 ',@insert_string1= 'asdes1 ',@insert_string2= '21adfsad ',@insert_string3= 'dki12 ',@insert_string4= 'qreopiu12 ',@insert_string5= 'asd98374 ' 
    你很牛哦,一眼就看出来了我想了二天了
      

  9.   

    @insertnumber 这个变量都没用到啊,有什么用?
      

  10.   

    试试这个 :
    exec   insert_new   
    'HouseNewAssisPicTable ',1, 'NewPicturePath ', 'NewPicturePath1 ', 'NewPicturePath2 ', 'NewPicturePath3 ', 
    'asdf12 ', '21adfsad ','dki12 ', 'qreopiu12 '
      

  11.   

    还是不行窝,老问题:类型转换错误!!!
    我要插入的表的表结构:
    ----------------------------------------------------
    Table: 
          dbo.HouseNewAssisPicTable 
          NewId   (bigint   ,notnull) 
          NewPicturePath   (char(100),not   null) 
          NewPicturePath1   (char(100),not   null) 
          NewPicturePath2   (char(100)) 
          NewPicturePath3   (char(100)) 
          NewPicturePath4   (char(100)) 
    -----------------------------------------------------
    如果不能动态的话,一万张表我写一万个插入的存储过程???
    这就是我为什么想都动态的原因!!!
    帮忙解决下谢谢哈!
      

  12.   

    按照我这个语法就可以了set   @sql   =   'INSERT   INTO '
    + @inserttable
    +'( '+@insertstring+ ', '+@insertstring1+ ','+@insertstring2+','+@insertstring3 +') 
    values ( '+ '''+@insert_string+''' + ','+ '''+@insert_string1+'''+ ','+'''+@insert_string2+'''+ ','+'''+@insert_string3+'''+') ' 
    select   (@sql) 
    exec   (@sql)
      

  13.   

    上面那个有点问题,下面这个可以
    set   @sql   =   'INSERT   INTO '
    + @inserttable
    +'( '+@insertstring+ ', '+@insertstring1+ ','+@insertstring2+','+@insertstring3 +') 
    values ( '+ '''' + @insert_string+'''' + ','+ ''''+@insert_string1+''''+ ','+''''+@insert_string2+''''+ ','+''''+@insert_string3+''''+') ' 
    select   (@sql) 
    exec   (@sql)
      

  14.   

    不知道你还在不在哦
    还是有问题?
    要插入数据的表结构:
    ------------------------------------------------------------------------------------ 
    Table: 
          dbo.HouseNewAssisTable
          NewId   (bigint   ,notnull) 
          NewTalkAbout(char(10)) 
          NewAbout(char(100),not   null) 
          NewTalkTime(datetime,not null)  
    ------------------------------------------------------------------------------------ 
    ------------------------------------------------------------------------------------ 
    执行: 
    exec insert_new       
    'HoseNewAssisTable   ',1,   'NewId   ',   'NewTalkAbout   ',   'NewAbout   ',   'NewTalkTime   ',   
    '222   ',   '21adfsad   ', 'dki12   ',   '2007-02-03   '-------------------------------------------------------------------------------------- 运行结果:
    --------------------------------------------------------------------------------------.
    服务器: 消息 208,级别 16,状态 1,行 1
    对象名 'HoseNewAssisTable' 无效。
    --------------------------------------------------
    ?????
    你在你的TS上测试过吗?
      

  15.   

    在字符串編輯的時候把@countnumber轉換成VARCHAR 類型試一下