帮我看看这个带输出参数的存储过程哪里写错了?

zhangfeigreat 2009-06-18 01:28:41
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE up_inquireTableExist
-- Add the parameters for the stored procedure here
@tableName char(6),
@s_exist bit output
AS
IF OBJECT_ID('@tableName','U') IS NOT NULL
@s_exit = 0
ELSE @s_exit = 1
GO
...全文
36 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangfeigreat 2009-06-18
  • 打赏
  • 举报
回复
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[up_inquireTableExist]
-- Add the parameters for the stored procedure here
@tableName char(6),
@s_exist int output
AS
--下面用动态SQL语句实现
declare @sql varchar(1000)
Set @sql = 'IF OBJECT_ID( ' + @tableName + ','U') IS NOT NULL
Set @s_exist = 1
ELSE
Set @s_exist = 0'
exec(@sql)

我改成上述动态语句,结果出现错误:‘U’附近有语法错误
应该怎么改呢
Rotel-刘志东 2009-06-18
  • 打赏
  • 举报
回复
CREATE PROCEDURE up_inquireTableExist 
-- Add the parameters for the stored procedure here
@tableName char(6),
@s_exist bit output
AS
IF exists ( select 1 from sys.tables where name=@tableName
set @s_exit = 0
ELSE
set @s_exit = 1
GO
zhangfeigreat 2009-06-18
  • 打赏
  • 举报
回复
采用8楼的代码出现了错误:必须声明标量@s_exist
yuangang1011 2009-06-18
  • 打赏
  • 举报
回复

cm.Parameters.AddWithValue("@s_exist", 0);
cm.Parameters["@s_exist"].DbType = DbType.Bit;
cm.Parameters["@s_exist"].Direction = ParameterDirection.Output;
cm.ExecuteNonQuery();

if (int.Parse(cm.Parameters["@BudgetError"].Value.ToString()) == 1)
{
.......
}


参考
zhangfeigreat 2009-06-18
  • 打赏
  • 举报
回复
这个问题解决了
那我在C#或者其它语言中调用这个存储过程,怎么获取这个输出参数@s_exist呢
谢谢
chowyi 2009-06-18
  • 打赏
  • 举报
回复
你声明的变量名写错了
等不到来世 2009-06-18
  • 打赏
  • 举报
回复
CREATE PROCEDURE up_inquireTableExist 
-- Add the parameters for the stored procedure here
@tableName char(6),
@s_exist bit output
AS
IF OBJECT_ID(@tableName,'U') IS NOT NULL --表名变量不用单引号
set @s_exist = 0 --缺少set
ELSE
set @s_exist = 1
GO
chowyi 2009-06-18
  • 打赏
  • 举报
回复
alter PROCEDURE dbo.up_inquireTableExist
@tableName char(6),
@s_exist bit output
AS
begin
IF OBJECT_ID('@tableName','U') IS NOT NULL
select @s_exist =0
ELSE select @s_exist =1
select @S_exist
end
yuangang1011 2009-06-18
  • 打赏
  • 举报
回复
建议用动态SQL
  • 打赏
  • 举报
回复
表名也不对。如果动态的,建议采用动态语句。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE up_inquireTableExist
-- Add the parameters for the stored procedure here
@tableName char(6),
@s_exist bit output
AS
declare @sql varchar(1000)
SET @sql='
IF OBJECT_ID('''+@tableName+''',''U'') IS NOT NULL
SET @s_exit = 0
ELSE
SET @s_exit = 1'
print @sql
exec(@sql)
GO
SQL77 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yuangang1011 的回复:]
SQL codeCREATE PROCEDURE up_inquireTableExist
-- Add the parameters for the stored procedure here
@tableName char(6),
@s_exist bit output
AS
IF OBJECT_ID('@tableName','U') IS NOT NULL
Set @s_exit = 0 -- 这里
ELSE
Set @s_exit = 1
GO
[/Quote]

UP
Yang_ 2009-06-18
  • 打赏
  • 举报
回复
CREATE PROCEDURE up_inquireTableExist 
-- Add the parameters for the stored procedure here
@tableName char(6),
@s_exist bit output
AS
IF exists ( select 1 from sys.tables where name=@tableName
set @s_exit = 0
ELSE
set @s_exit = 1
GO
  • 打赏
  • 举报
回复
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE up_inquireTableExist
-- Add the parameters for the stored procedure here
@tableName char(6),
@s_exist bit output
AS
IF OBJECT_ID('@tableName','U') IS NOT NULL
SET @s_exit = 0
ELSE
SET @s_exit = 1
GO


楼主忘了SET了。
yuangang1011 2009-06-18
  • 打赏
  • 举报
回复
SET ANSI_NULLS ON 
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE up_inquireTableExist
-- Add the parameters for the stored procedure here
@tableName char(6),
@s_exist bit output
AS
IF OBJECT_ID('@tableName','U') IS NOT NULL
Set @s_exist = 0
ELSE
Set @s_exist = 1
GO
SQL77 2009-06-18
  • 打赏
  • 举报
回复

IF OBJECT_ID('@tableName','U') IS NOT NULL
@s_exit = 0
ELSE @s_exit = 1
chowyi 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用楼主 zhangfeigreat 的帖子:]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE up_inquireTableExist
-- Add the parameters for the stored procedure here
@tableName char(6),
@s_exist bit output
AS
IF OBJECT_ID('@tableName','U') IS NOT NULL
@s_exit = 0
ELSE @s_exit = 1
GO

[/Quote]
把'@tableName'两边的''去掉
yuangang1011 2009-06-18
  • 打赏
  • 举报
回复
CREATE PROCEDURE up_inquireTableExist 
-- Add the parameters for the stored procedure here
@tableName char(6),
@s_exist bit output
AS
IF OBJECT_ID('@tableName','U') IS NOT NULL
Set @s_exit = 0 -- 这里
ELSE
Set @s_exit = 1
GO

34,873

社区成员

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

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