If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Check for table and return true or false

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-28-03, 11:27
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
Check for table and return true or false

How can I write a stored procedure to return if a table exist or not?

I put:

Code:
CREATE procedure sp_BA_ReportExist

(
@ISYES VARCHAR (10),
@ISNO VARCHAR (10)
)

AS

DECLARE @SQL varchar(8000)
SET @SQL = " if object_id('BA_REPORT_MASTER') is not null  RETURN  "+@ISYES+"  ELSE RETURN "+@ISNO+" "

EXEC(@SQL)
GO
I ran it with: sp_BA_ReportExist '1','0'

but I get:

Code:
Server: Msg 178, Level 15, State 1, Line 1
A RETURN statement with a return value cannot be used in this context.
Server: Msg 178, Level 15, State 1, Line 1
A RETURN statement with a return value cannot be used in this context.
How can I make this work?

Thanks!

Ken
Reply With Quote
  #2 (permalink)  
Old 02-28-03, 11:32
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
Figured it out... this works:


Code:

CREATE procedure sp_BA_ReportExist

(
@ISYES VARCHAR (10),
@ISNO VARCHAR (10)
)

AS

DECLARE @SQL varchar(8000)
SET @SQL = " if object_id('BA_REPORT_MASTER') is not null  PRINT  "+@ISYES+"  ELSE PRINT "+@ISNO+" "

EXEC(@SQL)
GO
As always I find the answer right after I post!
Reply With Quote
  #3 (permalink)  
Old 02-28-03, 12:29
Paul Young Paul Young is offline
Registered User
 
Join Date: Feb 2002
Location: Houston, TX
Posts: 809
you could also use:

declare @TableName sysname
set @TableNAme = 'sysobjects'
if OBJECTPROPERTY(OBJECT_ID(@TableName),'IsTable') = 1
print "+@ISYES+"
else
print "+@ISNO+"
__________________
Paul Young
(Knowledge is power! Get some!)
Reply With Quote
  #4 (permalink)  
Old 02-28-03, 12:30
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
I have another problem now...

How do I get the return value?
Reply With Quote
  #5 (permalink)  
Old 02-28-03, 12:32
Paul Young Paul Young is offline
Registered User
 
Join Date: Feb 2002
Location: Houston, TX
Posts: 809
do you want it as a result set, output parameter or as a numeric valued returned by the "RETURN" statement?
__________________
Paul Young
(Knowledge is power! Get some!)
Reply With Quote
  #6 (permalink)  
Old 02-28-03, 12:34
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
A resultset will work.

Basically I just need to know if the table exists so my application can set some values. Ic na't figure out how to get the value back into the application.

Thanks so much for any light you can shed on this!

Ken
Reply With Quote
  #7 (permalink)  
Old 02-28-03, 12:39
Paul Young Paul Young is offline
Registered User
 
Join Date: Feb 2002
Location: Houston, TX
Posts: 809
try:

Code:
create procedure sp_BA_ReportExist(
  @ISYES VARCHAR (10)
, @ISNO  VARCHAR (10))
AS
if (object_id('BA_REPORT_MASTER') is not null)
  select @ISYES as Answer
else
  select @ISNO as Answer

return 0
GO

exec sp_BA_ReportExist 'Yes', 'No'
__________________
Paul Young
(Knowledge is power! Get some!)
Reply With Quote
  #8 (permalink)  
Old 02-28-03, 12:42
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
Too Cool! Thank you so much!

I was kinda close, but didn't have it quite right!

Thanks for your help!

Ken
Reply With Quote
  #9 (permalink)  
Old 02-28-03, 12:43
Paul Young Paul Young is offline
Registered User
 
Join Date: Feb 2002
Location: Houston, TX
Posts: 809
or:
Code:
create procedure sp_TableExists(
  @TableName sysname
, @ISYES     VARCHAR (10) = 'Yes'
, @ISNO      VARCHAR (10) = 'No')
AS
select case OBJECTPROPERTY(OBJECT_ID(@TableName),'IsTable') when 1 then @ISYES else @ISNO end as Answer
return 0
GO

exec sp_TableExists 'sysobjects','Yes', 'No'

or just 

exec sp_TableExists 'sysobjects'
__________________
Paul Young
(Knowledge is power! Get some!)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On