Wednesday, March 7, 2012

Deferrend Name Resolution for a field in a Stored Procedure

We are doing an upgrade in about a month and changing the account structure
in one of our tables. I am trying to write a routine that will check to see
if we are using the old format or the new format. So I wrote the following
stored procedure:
CREATE PROCEDURE [dbo].[sp_Account_Info] AS
if exists(select COLUMN_NAME = convert(sysname,name) from syscolumns where
name ='ACTNUMBR_6')
begin
select ACTNUMBR_5,ACTNUMBR_6 from Account_Table
--Do more stuff
end
else
begin
select ACTNUMBR_4 from Account_Table
--Do more stuff
end
In the old format the columns stop at ACTUNUMBR_4, but in the new table
structure(Which has not been implmented yet) we will be adding ACTNUMBR_5
and ACTNUMBR_6. The problem I am having is SQLServer will not let me save
this stored procedure because it says I have an invalid column name. Is
there a way to save the stored procedure even though the new columns do not
yet exist?You could use dynamic SQL (www.sommarskog.se), but perhaps you should consid
er a stable data model
instead, which doesn't require you to add columns over time.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Miles C" <mcousens@.clearwater.ca> wrote in message news:eZmKRAHlGHA.408@.TK2MSFTNGP03.phx.g
bl...
> We are doing an upgrade in about a month and changing the account structur
e in one of our tables.
> I am trying to write a routine that will check to see if we are using the
old format or the new
> format. So I wrote the following stored procedure:
> CREATE PROCEDURE [dbo].[sp_Account_Info] AS
> if exists(select COLUMN_NAME = convert(sysname,name) from syscolumns where
name ='ACTNUMBR_6')
> begin
> select ACTNUMBR_5,ACTNUMBR_6 from Account_Table
> --Do more stuff
> end
> else
> begin
> select ACTNUMBR_4 from Account_Table
> --Do more stuff
> end
> In the old format the columns stop at ACTUNUMBR_4, but in the new table st
ructure(Which has not
> been implmented yet) we will be adding ACTNUMBR_5 and ACTNUMBR_6. The pro
blem I am having is
> SQLServer will not let me save this stored procedure because it says I hav
e an invalid column
> name. Is there a way to save the stored procedure even though the new col
umns do not yet exist?
>

No comments:

Post a Comment