WorkaHolic

쿼리 문장 자동 생성 스크립트

2010. 4. 8. 22:34
반응형

--SQL 7.0

 

Use Master
go

Create Procedure sp_Select
 @TableName varchar(130)
AS
Begin
Declare @OID int,@Name varchar(130),@SQL varchar(4000)

/* get the Object_id for the table */
 Select @OID = object_id(@TableName)

 If @OID is null
 Begin
  /* return the results in a column called SQL */
  Select SQL='Table Not Found'
  Return
 End

 Select @SQL='Select '

 DECLARE table_Cursor CURSOR Local Fast_Forward FOR Select Name from syscolumns where Id=@OID order by colid
 OPEN table_cursor     /*open the cursor*/
 FETCH NEXT FROM table_cursor INTO @Name /*Get the 1st row*/
 WHILE @@fetch_status=0   /*set into loop until no more data can be found*/
 BEGIN
  IF not @@fetch_status = -2
  BEGIN
   Select @SQL=@SQL + @Name + ','  
  END
  FETCH NEXT FROM table_cursor INTO @Name /* get the next row*/
 END
 Close table_cursor
 DEALLOCATE table_cursor

 /* Now we need to pull the extra comma off the end */
 Select @SQL=substring(@SQL,1,Datalength(@SQL) -1)

 Select @SQL=@SQL + ' FROM ' + @TableName

 /* return the results */

 Select SQL=@SQL
End
Go

use pubs
go
execute sp_select 'jobs'

반응형