--你可以建立如下的测试环境,如果看不出来我的问题的所在
create database test_p
go
create table t
(
tId int,
tName varchar(50)
)
go
insert t values('1','a') 
insert t values('2','b')
insert t values('3','1')
go
select * from t
use test_p
go
create procedure P_test
(
@txtfield varchar(50)
)
as
begin
exec('select tId from t where tName='+@txtfield+'')
end
--为什么我运行下面的会出错呢?提示:“列名 'a' 无效。”
exec p_test 'a'
--再运行下面的,又提示:“在将 varchar 值 'a' 转换成数据类型 int 时失败。”
exec p_test '1'
上面的错误是为什么?!  'a'又为什么要转成int型?

解决方案 »

  1.   

    create database test_p
    go
    create table t
    (
    tId int,
    tName varchar(50)
    )
    go
    insert t values('1','a') 
    insert t values('2','b')
    insert t values('3','1')
    go
    select * from t
    use test_p
    go
    create procedure P_test
    (
    @txtfield varchar(50)
    )
    as
    begin
    exec('select tId from t where tName='''+@txtfield+'''') --修改這裡
    end
    GO
    exec p_test 'a'
    exec p_test '1'
      

  2.   


    --你可以建立如下的测试环境,如果看不出来我的问题的所在
    create database test_p
    go
    use test_p --放到前面來
    GO
    create table t
    (
    tId int,
    tName varchar(50)
    )
    go
    insert t values('1','a') 
    insert t values('2','b')
    insert t values('3','1')
    go
    select * from t
    --use test_p
    go
    create procedure P_test
    (
    @txtfield varchar(50)
    )
    as
    begin
    exec('select tId from t where tName='''+@txtfield+'''') --修改這裡
    end
    GO
    --为什么我运行下面的会出错呢?提示:“列名 'a' 无效。”
    exec p_test 'a'
    --再运行下面的,又提示:“在将 varchar 值 'a' 转换成数据类型 int 时失败。”
    exec p_test '1'
    GO
    Drop Table tUse master
    Drop database test_p--Result
    /*
    tId
    1tId
    3
    */
    /*
    Drop Proc p_test
    Use master
    Drop database test_p
    Drop Table t*/
      

  3.   

    --你這種情況也可以不用使用動態SQL語句--你可以建立如下的测试环境,如果看不出来我的问题的所在
    create database test_p
    go
    use test_p --放到前面來
    GO
    create table t
    (
    tId int,
    tName varchar(50)
    )
    go
    insert t values('1','a') 
    insert t values('2','b')
    insert t values('3','1')
    go
    select * from t
    --use test_p
    go
    create procedure P_test
    (
    @txtfield varchar(50)
    )
    as
    begin
    select tId from t where tName= @txtfield  --修改這裡
    end
    GO
    --为什么我运行下面的会出错呢?提示:“列名 'a' 无效。”
    exec p_test 'a'
    --再运行下面的,又提示:“在将 varchar 值 'a' 转换成数据类型 int 时失败。”
    exec p_test '1'
    GO
    Drop Table tUse master
    Drop database test_p--Result
    /*
    tId
    1tId
    3
    */
    /*
    Drop Proc p_test
    Use master
    Drop database test_p
    Drop Table t*/
      

  4.   

    多復了些代碼/*
    Drop Proc p_test
    Use master
    Drop database test_p
    Drop Table t*/這段不用看
      

  5.   

    exec('select tId from t where tName='''+@txtfield+'''')--兩個單引號代表一個'
      

  6.   

    playwarcraft,還要上班撒,明天才休息。 :)
      

  7.   

    顺便问下为什么有的人用 insert(N'') 这样插入数据,和insert('')直接有什么不同吗?
      

  8.   

    你查詢的時候,是這樣的select tId from t where tName='a'然後你拼結的語句中的內容,最後也要是這樣的。你錯誤的原因在於,拼結出來的語句掉了a前後的單引號。使用你原來的存儲過程執行exec p_test 'a'這個時,執行的實際語句是select tId from t where tName= a所以提示“列名 'a' 无效。”執行exec p_test '1'這個時,執行的實際語句是select tId from t where tName= 1所以提示“在将 varchar 值 'a' 转换成数据类型 int 时失败。”
      

  9.   

    junshanhudazhaxi() ( ) 信誉:100    Blog   加为好友  2007-04-07 15:00:18  得分: 0  
     
     
       顺便问下为什么有的人用 insert(N'') 这样插入数据,和insert('')直接有什么不同吗?
      
     
    ------------
    N是Unicode,防止亂碼的。如果你的系統是簡體的,插入繁體字的時候,如果使用第一種方式,數據沒有問題,使用第二種方式的話,數據庫的數據會變成"?"。
      

  10.   

    你可以測試一下這個例子Create Table TEST(Name Nvarchar(10))
    Insert TEST Select N'張三'
    Union All Select '張三'
    GO
    Select * From TEST
    GO
    Drop Table TEST
      

  11.   

    我测试了你上面的,没看出什么来啊!
    没有乱码之类的。我的数据库是sqlserver2005的。
      

  12.   

    知道這個東西的作用就OK了。我的是繁體的SQL 2000, 在存儲簡體字的時候不加N會變成?。可能是簡體的SQL可以支持繁體字吧。