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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Procedure

Author  Topic 

asm
Posting Yak Master

140 Posts

Posted - 2007-03-12 : 01:46:11
Hi

I want to display/delete the tables record from the database
from single query or procedure. I am trying this procedure to
display the record but error message given.. pls help how to
do this..


CREATe PROCEDURE spTables
AS
DECLARE @TName varchar(100)
BEGIN
--Declare the Cursor
DECLARE C1 CURSOR FOR SELECT Table_Name FROM INFORMATION_SCHEMA.TABLES WHERE Table_Type='BASE TABLE' ORDER BY Table_Name
--Open the Cursor
OPEN C1
--Fetch the Next Record from Table to Cursor
FETCH NEXT FROM C1 INTO @TName
--While Loop
WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT 'Table Name : '+ @TName
SELECT * FROM @TName
FETCH NEXT FROM C1 INTO @TName
END
CLOSE C1
DEALLOCATE C1
END
GO


Thanks

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-12 : 02:04:18
quote:
Originally posted by asm

Hi

I want to display/delete the tables record from the database
from single query or procedure. I am trying this procedure to
display the record but error message given.. pls help how to
do this..


CREATe PROCEDURE spTables
AS
DECLARE @TName varchar(100)
BEGIN
--Declare the Cursor
DECLARE C1 CURSOR FOR SELECT Table_Name FROM INFORMATION_SCHEMA.TABLES WHERE Table_Type='BASE TABLE' ORDER BY Table_Name
--Open the Cursor
OPEN C1
--Fetch the Next Record from Table to Cursor
FETCH NEXT FROM C1 INTO @TName
--While Loop
WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT 'Table Name : '+ @TName
exec('SELECT * FROM ' + @TName)
FETCH NEXT FROM C1 INTO @TName
END
CLOSE C1
DEALLOCATE C1
END
GO


Thanks





Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

asm
Posting Yak Master

140 Posts

Posted - 2007-03-12 : 02:18:55
Thanks for your help.. Can you tell how to store the result i get from select statement in
a given .txt file

thanks
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-03-12 : 02:25:19
Use BCP command-line tool with QUERYOUT option to do this.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -