Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 dynamic fields select

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-01-25 : 07:54:49
declare @tblMain table
(
ID varchar(12),
code varchar(20),
Field1 float,
Field2 float,
Field3 float,
Field4 float,
...
...

)

select * from tblMain
this sql returns the fields in the table.
i.e.
ID code Field1 Field2 Field3 Field4...

I would like to run a query as above but for every field I would like to add a text to it and ending up with:

ID code [Field1 Space] [Field2 Space] [Field3 Space] [Field4 Space] ...

Note that the fields are dynamic.

How is this done in select sql please?
Thanks

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-01-25 : 07:56:48
do it in the presentation layer. Don't do it in the db.

It's trivially easy to do further up the chain. Hard to do in the db and very bad form.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-01-25 : 08:09:11
quote:
Originally posted by Transact Charlie

do it in the presentation layer. Don't do it in the db.

It's trivially easy to do further up the chain. Hard to do in the db and very bad form.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION



sounds good but the fact is that the fields of three tables are retrieved inside an inner join select query. And these tables, all have the same field names.
How can they be distinguished?
Thanks
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-01-25 : 09:37:07
alias them. If you have to

BEGIN TRAN

DECLARE @tab TABLE (
[val] CHAR(1) NOT NULL
)
INSERT @tab VALUES ('a'), ('b')

SELECT
t1.[val] AS [t1Val]
, t2.[val] AS [t2Val]
FROM
@tab AS t1
CROSS JOIN @tab AS t2

ROLLBACK


I just saw this
quote:

Note that the fields are dynamic.


but that doesn't compare to the example you posted.

Or are you saying that *you don't know* the fields that will be retrieved by the query?

I think you'll need to post the actual query.

I think you are trying to do something that you probably shouldn't be doing in the db.

Are you dynamically pivoting? Just because you *can* do that in the db, it doesn't mean that you should. Again -- that is much better done in the presentation layer.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-25 : 09:56:27
sounds really like presentation issue to me. Where are you trying to show the data with display names?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-01-25 : 09:57:50
solved. thank you
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-01-25 : 10:10:29
quote:
Originally posted by arkiboys

solved. thank you


Care to share.....

Others may stumble across this thread in the years to come and may wonder.....

WHY, HOW, WHAT HAPPENED?

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -