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.
| Author |
Topic |
|
transcom_bcn
Starting Member
16 Posts |
Posted - 2006-06-12 : 10:48:10
|
| Hello, I'm trying to create a Stored Procedure who receives the table name as a parameter, then uses a cursor to obtain every column name and then builds a string like SELECT col1, col2, ... from TABLEIn fact that would be the same as SELECT * FROM table; but I can't do this, because I'll be using this stored procedure to loop through many tables that has different quantity of columns with a DTS, and if a specify the *, then the DTS wouldn't let me do the select with tables with different quantity of fields.Could you help me please, because my code isn't working:CREATE PROCEDURE dbo.stp_Test(@tablename AS VARCHAR(50))ASDECLARE @columnname varchar(50)DECLARE @strsql Nvarchar(500)DECLARE @query varchar(4000)SET NOCOUNT ONDECLARE c1 CURSOR FORSELECT column_name FROM information_schema.columnsWHERE table_name = @tablenameOPEN c1FETCH NEXT FROM c1 INTO @columnnameWHILE @@fetch_status = 0 BEGINIF (@strsql is null)BEGINSET @strsql=@columnnameENDELSEBEGINSET @strsql = @strsql + ',' + @columnnameENDFETCH NEXT FROM c1 INTO @columnnameENDCLOSE c1DEALLOCATE c1SELECT @query = 'SELECT ' + @strsql + ' FROM ' + @tablenameEXEC @querySET NOCOUNT OFFGO |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
sanjnep
Posting Yak Master
191 Posts |
Posted - 2006-06-12 : 10:57:14
|
| Try this:DECLARE @columnname varchar(50)DECLARE @strsql Nvarchar(500)DECLARE @query varchar(4000)DECLARE @tablename varchar(500) SET @tablename = 'AccessNumbers_dim'SET NOCOUNT ONDECLARE c1 CURSOR FORSELECT column_name FROM information_schema.columnsWHERE table_name = @tablenameOPEN c1FETCH NEXT FROM c1 INTO @columnnameWHILE @@fetch_status = 0 BEGINIF (@strsql is null)BEGINSET @strsql=@columnnameENDELSEBEGINSET @strsql = @strsql + ',' + @columnnameENDFETCH NEXT FROM c1 INTO @columnnameENDCLOSE c1DEALLOCATE c1SELECT @query = 'SELECT ' + @strsql + ' FROM ' + @tablenameEXEC (@query)Sanjeev Shrestha12/17/1963 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-12 : 11:04:20
|
| You dont need to use CursorMadhivananFailing to plan is Planning to fail |
 |
|
|
transcom_bcn
Starting Member
16 Posts |
Posted - 2006-06-12 : 11:12:53
|
quote: Originally posted by sanjnep Try this:DECLARE @columnname varchar(50)DECLARE @strsql Nvarchar(500)DECLARE @query varchar(4000)DECLARE @tablename varchar(500) SET @tablename = 'AccessNumbers_dim'SET NOCOUNT ONDECLARE c1 CURSOR FORSELECT column_name FROM information_schema.columnsWHERE table_name = @tablenameOPEN c1FETCH NEXT FROM c1 INTO @columnnameWHILE @@fetch_status = 0 BEGINIF (@strsql is null)BEGINSET @strsql=@columnnameENDELSEBEGINSET @strsql = @strsql + ',' + @columnnameENDFETCH NEXT FROM c1 INTO @columnnameENDCLOSE c1DEALLOCATE c1SELECT @query = 'SELECT ' + @strsql + ' FROM ' + @tablenameEXEC (@query)Sanjeev Shrestha12/17/1963
Thank you very much, but I have a new question:My problem is that I have about 40 tables, and almost all of them has different number of columns. After the selection I have to extract that information into an Excel file (or at least in a comma or pipe separated format). I have to use a 2 DTS's (one who loops between the tables) and the other that makes the extraction for every table (using this Stored Procedure). And if I use the 'Select * from ...' it extracs me ok the first table, but when the second come (and has a different number of columns that the first), then the extraction fails because the mapping for the transformations is different.So I supose that I need some kind of dynamic query in the stored procedure in order to avoid errors in the loop extraction.How can I do this ? |
 |
|
|
|
|
|
|
|