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 > Try/catch help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #16 (permalink)  
Old 11-13-12, 09:30
MCrowley MCrowley is online now
Wage drone 24601
 
Join Date: Jan 2003
Location: Massachusetts
Posts: 5,233
Oddly enough, I just looked up the steps to execution of a query in the SQL Server 2008 Internals book (highly recommended, by the way). The first step is Parsing both checks syntax, and builds a query tree for the optimizer to use later on. The second step is binding, and this is where you usually get the error message about a column name not being able to be bound. I am pretty sure parsing affects a whole batch, but binding appears to be statement specific, with the exception that the first failure to bind appears to fail the rest of the batch. I only did a little experimentation with it, so I can not call myself an authority.
Reply With Quote
  #17 (permalink)  
Old 11-13-12, 12:19
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 13,597
Just for the jolly factor, let me know if this works for you:
Code:
CREATE PROCEDURE d43_FirstName
AS SELECT FirstName FROM PREmployees
GO
CREATE PROCEDURE d43_fName
AS SELECT fName FROM PREmployees
GO
CREATE TABLE PREmployees (
   fName NVARCHAR(99) NOT NULL
)
GO

INSERT INTO PREmployees (fName)
   VALUES ('Pat')
GO 5

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
   WHERE  'fName' = COLUMN_NAME
      AND 'PREmployees' = TABLE_NAME)
   EXECUTE d43_fName
ELSE
   EXECUTE d43_FirstName
GO

DROP PROCEDURE d43_FirstName
GO
DROP PROCEDURE d43_fName
GO
DROP TABLE PREmployees
GO
-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #18 (permalink)  
Old 11-13-12, 14:55
PracticalProgram PracticalProgram is offline
Registered User
 
Join Date: Sep 2001
Location: Chicago, Illinois, USA
Posts: 598
Your code worked for me on both 2008 and 2008R2.

I also ran the following test successfully:

Code:
CREATE TABLE PREmployees (
   fName NVARCHAR(99) NOT NULL
)
GO

INSERT INTO PREmployees (fName)
   VALUES ('Pat')
GO 5

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
   WHERE  'fName' = COLUMN_NAME
      AND 'PREmployees' = TABLE_NAME)
   EXECUTE ('SELECT fName FROM PREmployees')
ELSE
   EXECUTE ('SELECT FirstName FROM PREmployees')
GO

DROP TABLE PREmployees
GO
__________________
Ken

Maverick Software Design

(847) 864-3600 x2
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