这种写法对吗?怎么改正?

Zi_Yuan 2009-01-12 02:05:07
select isnull(@dfje,0) as dfje+cast(@i as varchar(50))
...全文
94 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaJiaBing 2009-01-12
  • 打赏
  • 举报
回复

你想做什么样功能。。
把 declare @str varchar(50)=>declare @str decimal(20,2)
还有end后的那这语句不要了。。

Zi_Yuan 2009-01-12
  • 打赏
  • 举报
回复
declare @str varchar(50),@jfje numeric(18,0),@dfje numeric(18,0),@j int
set @str='select '
set @j=1
while(@j<61)
begin
set @str=@str + 'isnull(' + @jfje + ',0) as jfje' + @j + ',isnull(' + @dfje + ',0) as dfje' + @j + ','
set @j=@j+1
end
set @str=substring(@str,1,len(@str)-1)
exec(@str)

在描红的那一句提示错误:
将数据类型 varchar 转换为 numeric 时出错。

怎么解决?再次感谢!
Zi_Yuan 2009-01-12
  • 打赏
  • 举报
回复
改成这样对了,exec('select isnull(' + @dfje + ',0) as dfje' + @i)
谢谢!
csdyyr 2009-01-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 csdyyr 的回复:]
SQL codeexec('select isnull('+@dfje+',0) as dfje+cast('+rtrim(@i)+'as varchar(50))')
[/Quote]
想错了,参考其它楼
twgxzjk 2009-01-12
  • 打赏
  • 举报
回复
select rtrim(isnull(@dfje,0)) +cast(@i as varchar(50)) as dfje
百年树人 2009-01-12
  • 打赏
  • 举报
回复
select isnull(@dfje,0)+cast(@i as varchar(50))  as dfje
csdyyr 2009-01-12
  • 打赏
  • 举报
回复
动态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
bennyyyyy 2009-01-12
  • 打赏
  • 举报
回复
select (isnull(@dfje,'0')+cast(@i as varchar(50))) dfje
csdyyr 2009-01-12
  • 打赏
  • 举报
回复
exec('select isnull('+@dfje+',0) as dfje+cast('+rtrim(@i)+' as varchar(50))')

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧