Select Columns Conditionally in SQL Server -
This question is already an answer here
- 12 replies
I call each column I want to select it as its name is not included that Eid
is what I have tried to do:
select * from TABLE_NAME '% Id%'
What is the solution?
You can do it this way. Assume as table id, value 1, value 2. You need to select the name of the column name like the
value .
CREATE TABLE [dbo]. [Tbl1] ([id] [int] detection (1,1) No NULL, [value1] [varchar] (2) zero, [value 2] [varchar] (4) zero) table #tblcols (colname varchar ( 100) Make GO) Select columns from INFORMATION_SCHEMA.COLUMNS insert in #tblcols where TABLE_NAME = N'tbl1 '#tblcols is removed where select colname'% val% '* #tblcols @sql nvarchar ( 500) Select @sql from announcement = colass set from COALESCE (@sql + ',', ') + #tblcols set @sql = + @sql +' tbl1 to '@sql executive sp_executesql selection @sql table #tblcols drop Then select the SQL statement like dynamic SQL
by using the 'select'
Use the
EXEC SP_EXECUTESQL @sQL to get the result of value selection 1, value 2 to tbl .
Comments
Post a Comment