tags:

views:

99

answers:

4

How can I write a stored procedure in SQL Server 2005 so that i can display the repeated column names by having a prefix added to it?

Example: If I have 'Others' as the column name belonging to a multiple categories mapped to another table having columns as 'MyColumn','YourColumn'. I need to join these two tables so that my output should be 'M_Others' and 'Y_Others'. I can use a case but I am not sure of any other repeated columns in the table. How to write that dynamically to know the repetitions ?

Thanks In Advance

+1  A: 

You should use aliases in the projection of the query: (bogus example, showing the usage)

SELECT c.CustomerID AS Customers_CustomerID, o.CustomerID AS Orders_CustomerID
FROM Customers c INNER JOIN Orders o ON c.CustomerID = o.CustomerID
Frans Bouma
thanks for the rlybut the thing here is i am not sure of the repititive columns that are there in the databse.if any repition of colum nae exists then i need to have a prefix that should be taken from the column which fromthe joining table column
I guess your query is written as : SELECT * FROM ... ? In that case, please reconsider writing out the query in full. This will automatically solve your problem, because you can write out the column names. You're writing a stored procedure so the stored procedure is fixed, which means it won't change. So write out the names of the columns. Sure if someone changes the tables used, your proc will fail, but that's unavoidable unless the person changing the table communicates with the person writing the procs.
Frans Bouma
+1  A: 

You can't dynamically change the column names without using dynamic SQL.

You have to explicitly alias them. There is no way to change "A_Others" or "B_Others" in this query:

SELECT
    A.Others AS A_Others,
    B.Others AS B_Others
FROM
    TableA A
    JOIN
    TableB B ON A.KeyCol = B.KeyCol
gbn
select M.ID_PK, (case when N.NAME ='others' then substring(M.NAME,1,1)+'_'+ N.NAME ELSE N.NAME end) from TEST_TABLE1 M with (NOLOCK) left join TEST_TABLE2 N on M.ID_PK=N.ID_FK this is the query where we are chesking for the repeted column name but here it is hard coded.if there exists another repeted column which we are not aware of then an errror will occur if we use this query in the stored procure.so is there any way to find out the repitions which are dynamically donbe at back end and need to be handled in a stored procedure which is a static one,
You want to find all columns in the database with the same name? Or ensure that the stored proc output gives unique names?
gbn
i want all the column names and the stored procedure should append a prefix to the repeted columns and dispaly the other columns along with them with out any prefix appended to it.only the repeated columns need to be appended with the prefixSELECT M.ID_PK, (CASE WHEN N.NAME --occurs for multiple times-- THEN substring(M.NAME,1,1)+'_'+ N.NAME ELSE N.NAME end) FROM TEST_TABLE1 M WITH (NOLOCK) LEFT JOIN TEST_TABLE2 N ON M.ID_PK=N.ID_FK in this query i need to have a condition to get the repeated columns
A: 

select n.id_pk, (case when groupcount.n_count > 1 then substring(m.name, 1, 1) + '_' + n.name else n.name end) from test_table1 m
left join test_table2 n on m.id_pk = n.id_fk
left join (select name, count(name) as n_count from test_table2 group by name) groupcount on n.name = groupcount.name

+1  A: 

If the repeated columns contain the same data (i.e. they are the join fields), you should not be sending both in the query anyway as this is a poor practice and is wasteful of both server and network resources. You should not use select * in queries on production especially if there are joins. If you are properly writing SQL code, you would alias as you go along when there are two columns with the same name that mean different things (for instance if you joined twice to the person table, once to get the doctor name and once to get the patient name). Doing this dynamically from system tables would not only be inefficient but could end up giving you a big security hole depending on how badly you wrote the code. You want to save five minutes or less in development by permanently affecting performance for every user and possibly negatively impacing data security. This is what database people refer to as a bad thing.

HLGEM