[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

Create columns in Select Statement

Asked by jpaulino in SQL Server 2005, SQL Query Syntax, MS SQL DTS

Tags: SQL

Hi,

I want to create columns dynamically in a select statement. I have a field name like this abc,def,ghi,jkl,& and I want the result in columns:

SELECT a,b,c <columns here> FROM myTable

The problem is that I dont want to do it manually (using SUBSTRING, MID, LEFT, ETC), because I dont know how many I will have.

Right know Im trying to use a Function, but no luck till now.

ALTER FUNCTION dbo.myFunction
(@prc VARCHAR(MAX))      

RETURNS VARCHAR(MAX)
AS
      BEGIN
      
      DECLARE @total INT
      DECLARE @actual INT
      DECLARE @counter INT
      
      DECLARE @SQL VARCHAR(MAX)
      
      SET @SQL = ''
      SET @total = LEN(@prc)
      SET @actual = 1
      SET @counter = 1
      
      WHILE (@actual+3)  < @total
            BEGIN
              SET @SQL = @SQL + ', SUBSTRING(prc,' +  CAST((@actual) AS VARCHAR) + ',3) AS PRC' + CAST(@counter AS VARCHAR)
              SET @actual = @actual + 3
              SET @counter = @counter + 1
            END
      RETURN @SQL
      
END


Then I want it to use in the stored procedure:

      DECLARE @aaa VARCHAR(MAX)

      SET @aaa = 'SELECT ID, data, dbo. myFunction(prc) FROM myTable WHERE ID < 500'
      EXEC(@aaa)

The problem is that I get a string for that column and not the columns!!!

Any help ?
[+][-]10/30/09 04:45 AM, ID: 25701932Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/30/09 04:47 AM, ID: 25701949Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/30/09 04:52 AM, ID: 25701985Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/30/09 04:58 AM, ID: 25702024Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/30/09 05:00 AM, ID: 25702037Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/30/09 05:07 AM, ID: 25702088Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/30/09 05:09 AM, ID: 25702098Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/30/09 06:20 AM, ID: 25702632Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/30/09 07:22 AM, ID: 25703201Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/30/09 07:29 AM, ID: 25703276Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/02/09 12:24 AM, ID: 25717733Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/20/09 12:07 AM, ID: 25868337Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625