日期:2014-05-18  浏览次数:20423 次

sql 存储过程,,号问题啊,急....
SQL code

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

CREATE PROC [dbo].[AddAccountInfo](
@userName varchar(50),
@password varchar(50),
@TMStamp bigint,
@sync_id int
)
as
begin

declare @sql varchar(500)
set @sql='insert into AccountInfo values('+@userName+','+@password+','+@TMStamp+','+@sync_id+')'
             
print @sql
end



SQL code

set @sql='insert into AccountInfo values('''+@userName+''','''+@password+''','+@TMStamp+','+@sync_id+')'


@userName和@password是varchar类型,前后应该 几个,号啊,怎么老报错啊
SQL code
exec AddAccountInfo 'aa','xx',1,1

消息 8114,级别 16,状态 5,过程 AddAccountInfo,第 12 行
从数据类型 varchar 转换为 bigint 时出错。




------解决方案--------------------
SQL code

set @sql='insert into AccountInfo values('+@userName+','+@password+','+cast(@TMStamp as varchar(100))+','+cast(@sync_id as varchar(100))+')'

------解决方案--------------------
樓上各位都误导LZ了,变量要不要加引号要看情况,不是絕對的,像下面这种情况就要加引号

SQL code
set @sql='insert into AccountInfo values('''+@userName+''','''+@password+''','''+cast(@TMStamp as varchar(100))+''','''+cast(@sync_id as varchar(100))+''')'

Exec (@sql)