+ -
当前位置:首页 → 问答吧 → 存储过程问题!

存储过程问题!

时间:2011-11-17

来源:互联网

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER procedure [dbo].[cp] 


as
declare @testn1 int
declare @testn2 int

begin
create table nocp(学号 int,姓名 char(8),testn1 int,testn2 int)  

select row_number() over (order by @testn1 desc) as '名次',学号,姓名,testn1 into www from 地理 order by 学号
select row_number() over (order by @testn2 desc) as '名次',学号,姓名,testn2 into www2 from 地理 order by 学号
insert into nocp(学号,姓名,testn1,testn2) select www.学号,www.姓名,www.名次,www2.名次 from www,www2
end


分析的结果是:
命令已成功完成。

执行的结果是:
消息 208,级别 16,状态 6,过程 cp,第 15 行
对象名 'dbo.cp' 无效。


为什么不行!


作者: iamqdxy   发布时间: 2011-11-17

SQL code
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

Create procedure [dbo].[cp] 


as
declare @testn1 int
declare @testn2 int

begin
create table nocp(学号 int,姓名 char(8),testn1 int,testn2 int)  

select row_number() over (order by @testn1 desc) as '名次',学号,姓名,testn1 into www from 地理 order by 学号
select row_number() over (order by @testn2 desc) as '名次',学号,姓名,testn2 into www2 from 地理 order by 学号
insert into nocp(学号,姓名,testn1,testn2) select www.学号,www.姓名,www.名次,www2.名次 from www,www2
end

作者: roy_88   发布时间: 2011-11-17

Create procedure [dbo].[cp] 

改為創建

作者: roy_88   发布时间: 2011-11-17

你的执行语句发出来看看

作者: geniuswjt   发布时间: 2011-11-17

LZ说了
分析的结果是:
命令已成功完成。

应该是你执行的语句不对吧?或者切过库忘了切回来了?
引用楼主 iamqdxy 的回复:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER procedure [dbo].[cp]


as
declare @testn1 int
declare @testn2 int

begin
create table nocp(学号 int,姓名 char(8),testn1 int,testn2 int)
……

作者: geniuswjt   发布时间: 2011-11-17

WHERE www.学号=www2.学号--少了條件
SQL code


Create procedure [dbo].[cp] 


as
declare @testn1 int
declare @testn2 int

begin
create table nocp(学号 int,姓名 char(8),testn1 int,testn2 int)  

select row_number() over (order by @testn1 desc) as '名次',学号,姓名,testn1 into www from 地理 order by 学号
select row_number() over (order by @testn2 desc) as '名次',学号,姓名,testn2 into www2 from 地理 order by 学号
insert into nocp(学号,姓名,testn1,testn2) 
select 
    www.学号,
    www.姓名,
    www.名次,
    www2.名次 
from www,www2
WHERE www.学号=www2.学号
end

作者: roy_88   发布时间: 2011-11-17

你的
declare @testn1 int
declare @testn2 int
是干什么用的?
没赋值,查询语句又在用.........
如果是参数的话,要写在as前面,且不要用declare,如果是程序里用的话,写在begin后面,而且要赋值.

作者: qianjin036a   发布时间: 2011-11-17

如果是列名的话,更不能这样处理,得用动态语句来查询了.

作者: qianjin036a   发布时间: 2011-11-17

执行成功,为什么数据库里没有表nocp?

作者: iamqdxy   发布时间: 2011-11-17

問題不少,在存儲過程里建表,判斷都沒有一個
SQL code
Create procedure [dbo].[cp] 


as

BEGIN
    IF OBJECT_ID('nocp') IS NULL
        create table nocp(学号 int,姓名 char(8),testn1 int,testn2 int)  

    select row_number() over (order by testn1 desc) as '名次',学号,姓名,testn1 into www from 地理 order by 学号
    select row_number() over (order by testn2 desc) as '名次',学号,姓名,testn2 into www2 from 地理 order by 学号
    insert into nocp(学号,姓名,testn1,testn2) 
    select 
        www.学号,
        www.姓名,
        www.名次,
        www2.名次 
    from www,www2
    WHERE www.学号=www2.学号
end


作者: roy_88   发布时间: 2011-11-17

引用 8 楼 iamqdxy 的回复:

执行成功,为什么数据库里没有表nocp?


存儲過程問題多多,建表最好不要放在存儲過程里

作者: roy_88   发布时间: 2011-11-17

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


create procedure [dbo].[cp] 
@tbl varchar(8),
@testn1 varchar(8),
@testn2 varchar(8)

as


begin
create table nocp(学号 int,姓名 char(8),testn1 int,testn2 int)  

select row_number() over (order by @testn1 desc) as '名次',学号,姓名,testn1 into www from @tbl order by 学号
select row_number() over (order by @testn2 desc) as '名次',学号,姓名,testn2 into www2 from @tbl order by 学号
insert into nocp(学号,姓名,testn1,testn2) select www.学号,www.姓名,www.名次,www2.名次 from www,www2 

end


消息 1087,级别 15,状态 2,过程 cp,第 14 行
必须声明表变量 "@tbl"。
消息 1087,级别 15,状态 2,过程 cp,第 15 行
必须声明表变量 "@tbl"。




作者: iamqdxy   发布时间: 2011-11-17

表变量怎么声明,在哪声明?

作者: iamqdxy   发布时间: 2011-11-17