Friday, March 9, 2012

I need to make a minor change to a SQL Server script that is in production. Need advice so

Hello. I am not very strong with SQL server. But I know enough to get my way around. The reason I am asking for
help is that I need to change a SQL server script that resides on one of the production database servers of the
company I work for. I just need to add two new lines to the stored procedure.(See the lines that are bolded.) These
values are [MO_FAX],[MO_EMAIL].

Can someone tell me if there is a best way of going about doing this? Can I just simply open up SQL server and
quickly make the change? The big issue here is that this script is used in production. So I am just a little worried
about screwing something up. Anyway, I would appreciate some good advice on this?

/* Returns all data given the region and country */
ALTER PROCEDURE [dbo].[GetAllInfomration]

AS
SELECT
[COUNTRY],
[Company_NAME],
[CompanyAddress],
[CompanyPhone],

[MO_FAX]
[MO_EMAIL]

[CompanyFax],

FROM [InfoLookup].[dbo].[Company_Contact]

RETURN

Assuming the columns are in the same table, you need a comma after the column names.

/* Returns all data given the region and country */
ALTER PROCEDURE [dbo].[GetAllInfomration]

AS
SELECT
[COUNTRY],
[Company_NAME],
[CompanyAddress],
[CompanyPhone],

[MO_FAX],
[MO_EMAIL],

[CompanyFax],

FROM [InfoLookup].[dbo].[Company_Contact]

RETURN

|||

Fix comma, and I would also recommend to move you new columns to the end of the result set so if any of your production application use columns ID number instead of name it will not blow up.

SELECT
[COUNTRY],
[Company_NAME],
[CompanyAddress],
[CompanyPhone],
[CompanyFax],
[MO_FAX],
[MO_EMAIL]

FROM [InfoLookup].[dbo].[Company_Contact]

|||

Cool. Thank you for the advice, I will take it.

Just curious. Typically does it matter what order the columns are in? Specifically do they ever have to map exactly to the order of the columns defined in the data table?

For example if my table columns are in the following order

(PK) FirstName Varchar(40)

(FK) LastName Varchar(40)

Social Security Varchar(8)

Would a corresponding sql script need to be in the same order. i.e.

[]FirstName]

[Last Name]

Social Security.

Or can it for example be in the reverse order of the way it is defined in the table.

Social Security

[Last Name]

[Last Name]

|||No the order doesnt matter as long as your application is not referring to the columns by their position. I've seen some .NET code with datareader where the code accesses columns by their positions. Perhaps there are other scenarios other .NETters can explain..|||

Yes most programmers reference column by column order number when they try to get value from column because it is faster when by column name.

But as long as they use column names referencing to data it will work, but if you have no idea how you result is processed by other users just do not change columns order.

Sometimes is also possible that they have fixed schema for result from you SP and you new columns will generate errors if they will run your SP.

You also should not change column name format (small capital letters) because for example XML is very sensitive on it and it will not recognize columns correctly if they have different so Field is different then field for XML

Thanks

No comments:

Post a Comment