帮我看看这条SQL语句

finsoft_001 2008-04-13 09:18:46
帮我看看这条SQL语句
DECLARE @PageNum INT
SET @PageNum=5
DECLARE @UserID VARCHAR(20)
SET @UserID='ningbin'
EXEC('select top '+@PageNum+' LogID,LogTitle,LogContent,UserName,LogMenuName,LogDateTime
from tbl_log inner join tbl_User on tbl_User.UserID=tbl_log.UserID
inner join tbl_LogMenu on tbl_LogMenu.LogMenuID=tbl_log.LogMenuID
where tbl_log.UserID='+ @UserID +'
ORDER BY LogDateTime DESC')

错误
服务器: 消息 207,级别 16,状态 3,行 1
列名 'ningbin' 无效。

表中 是有数据的
...全文
72 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2008-04-13
  • 打赏
  • 举报
回复
'''+ @UserID +'''' --子符串变量加上引号
kevinxiang 2008-04-13
  • 打赏
  • 举报
回复
好奇的问下怎么才可以把SQL代码复制上来。。。。
dawugui 2008-04-13
  • 打赏
  • 举报
回复
/*
在TOP后面使用变量
(爱新觉罗.毓华 2008-01-02 广东深圳)
*/

--SQL SERVER 2005 的写法
use adventureworks
go

DECLARE @Percentage int
SET @Percentage = 1
SELECT TOP (@Percentage) PERCENT
Name
FROM Production.Product
ORDER BY Name

/*
Name
----------------------
Adjustable Race
All-Purpose Bike Stand
AWC Logo Cap
BB Ball Bearing
Bearing Ball
Bike Wash - Dissolver

(6 行受影响)
*/

-----------------------------------
--SQL SERVER 2000 的写法
create table a([id] [int])
insert into a(id) values(1)
insert into a(id) values(2)
insert into a(id) values(3)
insert into a(id) values(4)
insert into a(id) values(5)

declare @num as int
declare @sql as varchar(2000)
set @num = 2
set @sql = 'select top ' + cast(@num as char) + ' * from a'
exec(@sql)

drop table a
/*
id
-----------
1
2
*/
Limpire 2008-04-13
  • 打赏
  • 举报
回复
where tbl_log.UserID='+ @UserID +'
-->
where tbl_log.UserID='''+ @UserID +'''
Limpire 2008-04-13
  • 打赏
  • 举报
回复
DECLARE @PageNum INT
SET @PageNum=5
DECLARE @UserID VARCHAR(20)
SET @UserID='ningbin'
EXEC('select top '+@PageNum+' LogID,LogTitle,LogContent,UserName,LogMenuName,LogDateTime
from tbl_log inner join tbl_User on tbl_User.UserID=tbl_log.UserID
inner join tbl_LogMenu on tbl_LogMenu.LogMenuID=tbl_log.LogMenuID
where tbl_log.UserID='''+ @UserID +'''
ORDER BY LogDateTime DESC')

34,593

社区成员

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

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