--你可以建立如下的测试环境,如果看不出来我的问题的所在
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型?
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型?
解决方案 »
- asp中对象无法关闭该怎么解决?
- 能够根据字段值,查找出该字段值位于数据库哪张表,甚至哪一行?
- 如何将excel 导入sql中,而不依附其他编程语言呢
- 关于一次性建立字段完全相同的多个表
- 请问如何 用 触发器 限制 某个 字段不能被 更新 ??????????? 急!!!!!!!!!!!!
- 请求一条sql语句
- 存储过程的奇怪问题,求解。
- 请问视图里面能不能带参数的?
- 求computer 以及computer by 的实例,最好有建表以及表数据,谢谢了!
- --==关与动网Sql版的问题,我用标准的sql连接方式没问题,但如果换成DSN连接则不行(在线等)==—-
- 在2003上装SQL2000提示112错误,不能安装,怎么弄?
- 弱弱的问一句,我这个存储过程哪里错了?
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'
--你可以建立如下的测试环境,如果看不出来我的问题的所在
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*/
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*/
Drop Proc p_test
Use master
Drop database test_p
Drop Table t*/這段不用看
顺便问下为什么有的人用 insert(N'') 这样插入数据,和insert('')直接有什么不同吗?
------------
N是Unicode,防止亂碼的。如果你的系統是簡體的,插入繁體字的時候,如果使用第一種方式,數據沒有問題,使用第二種方式的話,數據庫的數據會變成"?"。
Insert TEST Select N'張三'
Union All Select '張三'
GO
Select * From TEST
GO
Drop Table TEST
没有乱码之类的。我的数据库是sqlserver2005的。