sqlserver求助

wangli00000 2018-10-18 11:57:13
--指定数据库
use 试验
go

-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'select *
from table1
where
case
when '+@c1+' in ('压折比') then '+@c1+' <= 3
when '+@c1+' in ('拉伸粘结强度') then '+@c1+' >= 0.5'
print @string1
exec(@string1)
go

exec proc1 '拉伸粘结强度'

就是表只有1列,列名作为参数@c1,当列名为拉伸粘结强度时,选取拉伸粘结强度>=0.5的数据;当列名为压折比时,选取压折比<=3的数据

错误消息为:
消息 102,级别 15,状态 1,过程 proc1,第 22 行
'压折比' 附近有语法错误。
消息 2812,级别 16,状态 62,第 2 行
找不到存储过程 'proc1'。

不知道错在哪了,有哪位大虾帮看看么
...全文
622 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_43468561 2018-10-19
  • 打赏
  • 举报
回复
你只需要把这句很重要的话 —— “I'm gonna WIN!”——连续输出三遍就可以了。 注意每遍占一行,除了每行的回车不能有任何多余字符。
RINK_1 2018-10-19
  • 打赏
  • 举报
回复
引用 15 楼 wangli00000 的回复:
表结构在4楼 我改了下语句: --指定数据库 use 试验 go -- 判断要创建的存储过程名是否存在 if exists (select * from sys.objects where name = 'proc1') -- 删除存储过程 drop proc proc1 go --存储过程proc1 create proc proc1 @c1 varchar(50) as --临时表 declare @string1 varchar(max) set @string1 = 'if '+@c1+' in (''压折比'') select * from table1 where '+@c1+' <= 3 else if '+@c1+' in (''拉伸粘结强度'') select * from table1 where '+@c1+' >= 0.5 ' print @string1 exec(@string1) go exec proc1 '拉伸粘结强度'
结果为: if 拉伸粘结强度 in ('压折比') select * from table1 where 拉伸粘结强度 <= 3 else if 拉伸粘结强度 in ('拉伸粘结强度') select * from table1 where 拉伸粘结强度 >= 0.5 消息 207,级别 16,状态 1,第 1 行 列名 '拉伸粘结强度' 无效。 消息 207,级别 16,状态 1,第 4 行 列名 '拉伸粘结强度' 无效。

declare @string1 varchar(max)
declare @c1 varchar(50)
set @c1='拉伸粘结强度'
set @string1 =

'if exists (select 1 from table1 where '+@c1+' in (''压折比''))
begin
select *
from table1 where '+@c1+' <= 3
end
else if exists (select 1 from table1 where '+@c1+' in (''拉伸粘结强度''))
begin
select *
from table1 where '+@c1+' >= 0.5
end'
print @string1
exec(@string1)

RINK_1 2018-10-18
  • 打赏
  • 举报
回复

set @string1 =

'select *
from table1
where
case
when '+@c1+' in (''压折比'') then '+@c1+' <= 3
when '+@c1+' in (''拉伸粘结强度'') then '+@c1+' >= 0.5'
print @string1
exec(@string1)
卖水果的net 2018-10-18
  • 打赏
  • 举报
回复
表结构也贴上来。 楼主可能是个新手,不贴表结构,后续的错误,也会困扰你的。
wangli00000 2018-10-18
  • 打赏
  • 举报
回复
表结构在4楼
我改了下语句:
--指定数据库
use 试验
go

-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'if '+@c1+' in (''压折比'')
select *
from table1 where '+@c1+' <= 3
else if '+@c1+' in (''拉伸粘结强度'')
select *
from table1 where '+@c1+' >= 0.5 '

print @string1
exec(@string1)
go

exec proc1 '拉伸粘结强度'


结果为:
if 拉伸粘结强度 in ('压折比')
select *
from table1 where 拉伸粘结强度 <= 3
else if 拉伸粘结强度 in ('拉伸粘结强度')
select *
from table1 where 拉伸粘结强度 >= 0.5
消息 207,级别 16,状态 1,第 1 行
列名 '拉伸粘结强度' 无效。
消息 207,级别 16,状态 1,第 4 行
列名 '拉伸粘结强度' 无效。
RINK_1 2018-10-18
  • 打赏
  • 举报
回复
引用 5 楼 wangli00000 的回复:
按楼上2位朋友的方法,单引号改为双引号,输出结果为: select * from table1 where case when 拉伸粘结强度 in ('压折比') then 拉伸粘结强度 <= 3 when 拉伸粘结强度 in ('拉伸粘结强度') then 拉伸粘结强度 >= 0.5 消息 102,级别 15,状态 1,第 5 行 '<' 附近有语法错误。
这样试试呢

declare @string1 varchar(max)
declare @c1 varchar(50)
set @c1='拉伸粘结强度'
set @string1 =

'select *
from table1
where
(case
when '+@c1+' in (''压折比'') then '+@c1+'- 3
when '+@c1+' in (''拉伸粘结强度'') then 0.5-'+@c1+'
end)<=0'
print @string1
exec(@string1)
二月十六 2018-10-18
  • 打赏
  • 举报
回复
引用 12 楼 wangli00000 的回复:
[quote=引用 11 楼 sinat_28984567 的回复:]
[quote=引用 9 楼 wangli00000 的回复:]
[quote=引用 8 楼 sinat_28984567 的回复:]
改成这样试试
-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'select *
from table1
where
( '+@c1+' in (''压折比'') and '+@c1+' <= 3 ) or (
'+@c1+' in (''拉伸粘结强度'') and '+@c1+' >= 0.5)'
print @string1

GO

我的where 必须带case when的,in()里面其实不止一个字符串,还可能是字符串集合,我是报错了拿1个字符串来调试,我的表结构在4楼,版主能帮我看看我自己到底错在哪了么?[/quote]

case when --这里不能写判断 a>b这种的,只能写 a 最后end后边可以加 > b

为什么必须用case when ?可以多个字符串啊。[/quote]

我简单问一句:where case when condition1 then result1,这里面的result1不能写成a>=b??

我的目标就是:列名作为参数@c1,当列名为拉伸粘结强度时,选取拉伸粘结强度>=0.5的数据;当列名为压折比时,选取压折比<=3的数据
[/quote]
对,不能写成a>=b
wangli00000 2018-10-18
  • 打赏
  • 举报
回复
引用 11 楼 sinat_28984567 的回复:
[quote=引用 9 楼 wangli00000 的回复:]
[quote=引用 8 楼 sinat_28984567 的回复:]
改成这样试试
-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'select *
from table1
where
( '+@c1+' in (''压折比'') and '+@c1+' <= 3 ) or (
'+@c1+' in (''拉伸粘结强度'') and '+@c1+' >= 0.5)'
print @string1

GO

我的where 必须带case when的,in()里面其实不止一个字符串,还可能是字符串集合,我是报错了拿1个字符串来调试,我的表结构在4楼,版主能帮我看看我自己到底错在哪了么?[/quote]

case when --这里不能写判断 a>b这种的,只能写 a 最后end后边可以加 > b

为什么必须用case when ?可以多个字符串啊。[/quote]

我简单问一句:where case when condition1 then result1,这里面的result1不能写成a>=b??

我的目标就是:列名作为参数@c1,当列名为拉伸粘结强度时,选取拉伸粘结强度>=0.5的数据;当列名为压折比时,选取压折比<=3的数据
二月十六 2018-10-18
  • 打赏
  • 举报
回复
引用 9 楼 wangli00000 的回复:
[quote=引用 8 楼 sinat_28984567 的回复:]
改成这样试试
-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'select *
from table1
where
( '+@c1+' in (''压折比'') and '+@c1+' <= 3 ) or (
'+@c1+' in (''拉伸粘结强度'') and '+@c1+' >= 0.5)'
print @string1

GO

我的where 必须带case when的,in()里面其实不止一个字符串,还可能是字符串集合,我是报错了拿1个字符串来调试,我的表结构在4楼,版主能帮我看看我自己到底错在哪了么?[/quote]

case when --这里不能写判断 a>b这种的,只能写 a 最后end后边可以加 > b

为什么必须用case when ?可以多个字符串啊。
卖水果的net 2018-10-18
  • 打赏
  • 举报
回复
楼主说一下你的需求吧,这个 case when 的用法是有问题的。
wangli00000 2018-10-18
  • 打赏
  • 举报
回复
引用 8 楼 sinat_28984567 的回复:
改成这样试试
-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'select *
from table1
where
( '+@c1+' in (''压折比'') and '+@c1+' <= 3 ) or (
'+@c1+' in (''拉伸粘结强度'') and '+@c1+' >= 0.5)'
print @string1

GO

我的where 必须带case when的,in()里面其实不止一个字符串,还可能是字符串集合,我是报错了拿1个字符串来调试,我的表结构在4楼,版主能帮我看看我自己到底错在哪了么?
二月十六 2018-10-18
  • 打赏
  • 举报
回复
改成这样试试
-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'select *
from table1
where
( '+@c1+' in (''压折比'') and '+@c1+' <= 3 ) or (
'+@c1+' in (''拉伸粘结强度'') and '+@c1+' >= 0.5)'
print @string1

GO
wangli00000 2018-10-18
  • 打赏
  • 举报
回复
引用 3 楼 sinat_28984567 的回复:

-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'select *
from table1
where
case
when '+@c1+' in (''压折比'') then '+@c1+' <= 3
when '+@c1+' in (''拉伸粘结强度'') then '+@c1+' >= 0.5'
print @string1

GO

exec proc1 '拉伸粘结强度'




引用 3 楼 sinat_28984567 的回复:

-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'select *
from table1
where
case
when '+@c1+' in (''压折比'') then '+@c1+' <= 3
when '+@c1+' in (''拉伸粘结强度'') then '+@c1+' >= 0.5'
print @string1

GO

exec proc1 '拉伸粘结强度'




引用 3 楼 sinat_28984567 的回复:

-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'select *
from table1
where
case
when '+@c1+' in (''压折比'') then '+@c1+' <= 3
when '+@c1+' in (''拉伸粘结强度'') then '+@c1+' >= 0.5'
print @string1

GO

exec proc1 '拉伸粘结强度'




引用 3 楼 sinat_28984567 的回复:

-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'select *
from table1
where
case
when '+@c1+' in (''压折比'') then '+@c1+' <= 3
when '+@c1+' in (''拉伸粘结强度'') then '+@c1+' >= 0.5'
print @string1

GO

exec proc1 '拉伸粘结强度'




按版主的方法,单引号改为双引号,输出结果为:
select *
from table1
where
case
when 拉伸粘结强度 in ('压折比') then 拉伸粘结强度 <= 3
when 拉伸粘结强度 in ('拉伸粘结强度') then 拉伸粘结强度 >= 0.5
消息 102,级别 15,状态 1,第 5 行
'<' 附近有语法错误。
二月十六 2018-10-18
  • 打赏
  • 举报
回复
引用 5 楼 wangli00000 的回复:
按楼上2位朋友的方法,单引号改为双引号,输出结果为:
select *
from table1
where
case
when 拉伸粘结强度 in ('压折比') then 拉伸粘结强度 <= 3
when 拉伸粘结强度 in ('拉伸粘结强度') then 拉伸粘结强度 >= 0.5
消息 102,级别 15,状态 1,第 5 行
'<' 附近有语法错误。

case when的用法用错了,不能直接在里边写条件
wangli00000 2018-10-18
  • 打赏
  • 举报
回复
按楼上2位朋友的方法,单引号改为双引号,输出结果为:
select *
from table1
where
case
when 拉伸粘结强度 in ('压折比') then 拉伸粘结强度 <= 3
when 拉伸粘结强度 in ('拉伸粘结强度') then 拉伸粘结强度 >= 0.5
消息 102,级别 15,状态 1,第 5 行
'<' 附近有语法错误。
wangli00000 2018-10-18
  • 打赏
  • 举报
回复
二月十六 2018-10-18
  • 打赏
  • 举报
回复

-- 判断要创建的存储过程名是否存在
if exists (select * from sys.objects where name = 'proc1')
-- 删除存储过程
drop proc proc1
go

--存储过程proc1
create proc proc1
@c1 varchar(50)
as

--临时表
declare @string1 varchar(max)
set @string1 =

'select *
from table1
where
case
when '+@c1+' in (''压折比'') then '+@c1+' <= 3
when '+@c1+' in (''拉伸粘结强度'') then '+@c1+' >= 0.5'
print @string1

GO

exec proc1 '拉伸粘结强度'


22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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