SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Display the SELECT statement for any table


Display the SELECT statement for any table

Author
Message
Mehernosh
Mehernosh
SSC Veteran
SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)

Group: General Forum Members
Points: 278 Visits: 227
Comments posted to this topic are about the item Display the SELECT statement for any table

Mehernosh.
HextallFanForLife
HextallFanForLife
SSC-Enthusiastic
SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)SSC-Enthusiastic (146 reputation)

Group: General Forum Members
Points: 146 Visits: 238
You should add the schema of the table. That would be better.
Mehernosh
Mehernosh
SSC Veteran
SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)

Group: General Forum Members
Points: 278 Visits: 227
Fair point HextallFanForLife.

You can try the following if you want the Schema name to be included too.

DECLARE @mStrColumns VARCHAR(8000) , @mStrTable VARCHAR(100)
SET @mStrTable ='TABLE_NAME'
--
SELECT @mStrColumns = COALESCE(@mStrColumns + ', ', '') + '[' + CAST(SC.[NAME] AS VARCHAR(100) ) +']' ,@mStrTable = schema_Name(schema_id) + '.'TABLE_NAME'
FROM [SYSCOLUMNS] SC
JOIN SYS.OBJECTS SO ON SC.ID = SO.object_ID
WHERE SC.STATUS <> COALESCE (NULL ,SC.STATUS+1) AND SO.NAME = @mStrTable
--
SELECT 'SELECT ' + @mStrColumns + ' FROM ' + @mStrTable RESULTS



Please replace the "'TABLE_NAME" in the script with your Table Name.

Hope this helps :-)

Mehernosh.
Ken Hedges
Ken Hedges
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 5
Mehernosh (8/13/2013)
Fair point HextallFanForLife.

You can try the following if you want the Schema name to be included too.

DECLARE @mStrColumns VARCHAR(8000) , @mStrTable VARCHAR(100)
SET @mStrTable ='TABLE_NAME'
--
SELECT @mStrColumns = COALESCE(@mStrColumns + ', ', '') + '[' + CAST(SC.[NAME] AS VARCHAR(100) ) +']' ,@mStrTable = schema_Name(schema_id) + '.'TABLE_NAME'
FROM [SYSCOLUMNS] SC
JOIN SYS.OBJECTS SO ON SC.ID = SO.object_ID
WHERE SC.STATUS <> COALESCE (NULL ,SC.STATUS+1) AND SO.NAME = @mStrTable
--
SELECT 'SELECT ' + @mStrColumns + ' FROM ' + @mStrTable RESULTS



Please replace the "'TABLE_NAME" in the script with your Table Name.

Hope this helps :-)


I believe that you want the SELECT statement in the above script to read as follows:

SELECT @mStrColumns = COALESCE(@mStrColumns + ', ', '') + '[' + CAST(SC.[NAME] AS VARCHAR(100) ) +']' ,@mStrTable = schema_Name(schema_id) + '.' + 'TABLE_NAME'

Otherwise, there is an error and the command does not execute.

Of course, as the author explains, you should replace TABLE_NAME with the actual name of the table you're after.
Tom Hamilton
Tom Hamilton
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3575 Visits: 794
I got a syntax error from the text on the second example.

Change the line
SELECT @mStrColumns = COALESCE(@mStrColumns + ', ', '') + '[' + CAST(SC.[NAME] AS VARCHAR(100) ) +']' ,@mStrTable = schema_Name(schema_id) + '.'TABLE_NAME'

To:
SELECT @mStrColumns = COALESCE(@mStrColumns + ', ', '') + '[' + CAST(SC.[NAME] AS VARCHAR(100) ) +']' ,@mStrTable = schema_Name(schema_id) + '.' + @mStrTable

Tom in Sacramento
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
dennis.oconnor
dennis.oconnor
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 329
This is just what I need to keep me from using select * when doing a quick extract that sometimes ends up being used repeatedly.
below86
below86
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2154 Visits: 2862
Why don't you just right click on the table name in SSMS in the object explorer and do 'Script table as' then select 'SELECT to'. This will do the same thing plus format the results.

Am I missing something?

-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Mehernosh
Mehernosh
SSC Veteran
SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)

Group: General Forum Members
Points: 278 Visits: 227
Thanks Ken Hedges

---------------------------------
---Complete working example---
---Apologies for any mix ups.
---------------------------------


--Create a test table & insert test record.
Create Table TestSelect (tstField1 Varchar(10), tstField2 Varchar(10), tstField3 Varchar(10))
Insert into TestSelect Select 'A', 'A1', 'A100'

--Cheking the Table
SELECT * FROM TESTSELECT

---------------------------------
----Main query----
---------------------------------
DECLARE @mStrColumns VARCHAR(8000) , @mStrTable VARCHAR(100)
SET @mStrTable ='testSelect'
--
SELECT @mStrColumns = COALESCE(@mStrColumns + ', ', '') + '[' + CAST(SC.[NAME] AS VARCHAR(100) ) +']' ,@mStrTable = schema_Name(schema_id) + '.' + SO.NAME
FROM [SYSCOLUMNS] SC
JOIN SYS.OBJECTS SO ON SC.ID = SO.object_ID
WHERE SC.STATUS <> COALESCE (NULL ,SC.STATUS+1) AND SO.NAME = @mStrTable
--
SELECT 'SELECT ' + @mStrColumns + ' FROM ' + @mStrTable RESULTS

---The results as expected.
--SELECT [tstField1], [tstField2], [tstField3] FROM dbo.TestSelect

--Remember to drop the table afterwards.
Drop table testSelect



Hope this helps.

Mehernosh.
Mehernosh
Mehernosh
SSC Veteran
SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)

Group: General Forum Members
Points: 278 Visits: 227
Tom_Sacramento


---------------------------------
---Complete working example---
---Apologies for any mix ups.
---------------------------------


--Create a test table & insert test record.
Create Table TestSelect (tstField1 Varchar(10), tstField2 Varchar(10), tstField3 Varchar(10))
Insert into TestSelect Select 'A', 'A1', 'A100'

--Cheking the Table
SELECT * FROM TESTSELECT

---------------------------------
----Main query----
---------------------------------
DECLARE @mStrColumns VARCHAR(8000) , @mStrTable VARCHAR(100)
SET @mStrTable ='testSelect'
--
SELECT @mStrColumns = COALESCE(@mStrColumns + ', ', '') + '[' + CAST(SC.[NAME] AS VARCHAR(100) ) +']' ,@mStrTable = schema_Name(schema_id) + '.' + SO.NAME
FROM [SYSCOLUMNS] SC
JOIN SYS.OBJECTS SO ON SC.ID = SO.object_ID
WHERE SC.STATUS <> COALESCE (NULL ,SC.STATUS+1) AND SO.NAME = @mStrTable
--
SELECT 'SELECT ' + @mStrColumns + ' FROM ' + @mStrTable RESULTS

---The results as expected.
--SELECT [tstField1], [tstField2], [tstField3] FROM dbo.TestSelect

--Remember to drop the table afterwards.
Drop table testSelect



Hope this helps.

Mehernosh.
Mehernosh
Mehernosh
SSC Veteran
SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)

Group: General Forum Members
Points: 278 Visits: 227
below86

below86

Why don't you just right click on the table name in SSMS in the object explorer and do 'Script table as' then select 'SELECT to'. This will do the same thing plus format the results.

Am I missing something?



Perfectly fine and valid. You are not missing anything out.

Just wanted a SQL script to do the needful - hence the post.

Mehernosh.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search