Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to get the columns from respective tables in a database dynamically in SQL. i am able to fetch columns without square braces embarrassed. But I need to do so since there is are columns with hyphen for few tables .

What I have tried:

Core Code is as below:
select @SourceColumnList = @SourceColumnList + ',' + case when DATA_TYPE = 'xml' then 'convert(varchar(max),'+column_name+')' else column_name end 
from information_schema.columns 
where table_name = @TableName and DATA_TYPE not in ('timestamp') 

select @DestinationColumnList=  @DestinationColumnList+ ',' + column_name 
from information_schema.columns 
where table_name = @TableName and DATA_TYPE not in ('timestamp') 


set @SourceColumnList= RIGHT(@SourceColumnList,Len(@SourceColumnList)-1) 
set @DestinationColumnList= RIGHT(@DestinationColumnList,Len(@DestinationColumnList)-1)


select
set @SourceColumnList


select
set @DestinationColumnList
Posted
Updated 12-Jun-18 2:28am
Comments
Maciej Los 12-Jun-18 1:11am    
What?
ranio 12-Jun-18 3:08am    
I want to fetch the columns from table as in below format

I mean say

insert into test([a].[b],[c])
select [a],[b],[c] from abc

all columns must be with in square braces as mentioned above.
i need to set so in the dynamic script shown below:

select @SourceColumnList = @SourceColumnList + ',' + case when DATA_TYPE = 'xml' then 'convert(varchar(max),'+column_name+')' else column_name end
from information_schema.columns
where table_name = @TableName and DATA_TYPE not in ('timestamp')

select @DestinationColumnList= @DestinationColumnList+ ',' + column_name
from information_schema.columns
where table_name = @TableName and DATA_TYPE not in ('timestamp')


set @SourceColumnList= RIGHT(@SourceColumnList,Len(@SourceColumnList)-1)
set @DestinationColumnList= RIGHT(@DestinationColumnList,Len(@DestinationColumnList)-1)

Not sure I understand your question. Are you looking for a way to get the comma separated list or a way to add the brackets?

Does this perhaps solve your problem?

DECLARE @TableName varchar(max) = 'Invoices'
DECLARE @ColumnList varchar(max)
SELECT @ColumnList = COALESCE(@ColumnList + ',', '') + '[' + column_name + ']' from information_schema.columns WHERE table_name = @TableName
SELECT @ColumnList
 
Share this answer
 
This is what your sql query should look like
INSERT INTO [SOME_TABLE]
           ([Position]
           ,[DisplayText]
           ,[DisplayURL]
           ,[ToolTip])
     VALUES (SELECT Position, DisplayText, Display, URLToolTip
			 FROM  [SOME_OTHER_TABLE]
			 WHERE [Position] = @parent_id)


The value for the where clause should be a parameter in your c# code.
SqlParameter param = new SqlParameter
            {
                ParameterName = "@parent_id",
                DbType = DbType.Int32,
                Value = parentID
            }; 


and added to your SqlCommand object like this

cmd.Parameters.Add(param);
 
Share this answer
 
Comments
Richard Deeming 12-Jun-18 10:23am    
This solution doesn't have any connection to the question, other than the fact that it's SQL.

Were you trying to answer a different question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900