Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

sp_getrecords to get all records from a table Expand / Collapse
Author
Message
Posted Thursday, August 29, 2013 8:27 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:58 PM
Points: 36,794, Visits: 31,253
saravanakumar.G (8/28/2013)
any help on this???


Yes. If you insist on creating this proc, I recommend that you include a TOP 100 or TOP 1000 in the proc so that you're not using unnecessary resources. Using such a proc on big tables can really cause problems.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1489982
Posted Friday, August 30, 2013 1:19 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 5:09 AM
Points: 45, Visits: 139
Thanks for all of your reply :)

Yes. I tried to get the schema name for the table i pass and if there are more than one schema available(not many table name are available in more than one schema) then it will select the records from all the schema.

The code is below

create PROC [sp_getrecords]
(
@TableName sysname
)
AS
BEGIN

DECLARE @SchemaNames table(Row int identity(1,1), Sch sysname)

DECLARE @i int
DECLARE @SchemaName sysname
DECLARE @Str nvarchar(2000)

SELECT @i = 1

SELECT @Str = 'SELECT OBJECT_SCHEMA_NAME(s.object_id,DB_ID('''+db_name()+'''))
FROM '+db_name()+'.sys.objects s
WHERE
name = '''+@TableName+'''
AND
TYPE in (''u'', ''v'')
'

INSERT INTO @SchemaNames
EXEC SP_ExecuteSQL @Str

WHILE (@i <= (SELECT MAX(Row) FROM @SchemaNames))
BEGIN
SELECT @SchemaName = Sch FROM @SchemaNames
WHERE Row = @i
select @SchemaName
exec('select * from ['+@SchemaName+'].['+@TableName+']')

select @i= @i+1
END
END


Any optimization of this script is welcome.. :)
Post #1490018
Posted Friday, August 30, 2013 7:41 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:58 PM
Points: 36,794, Visits: 31,253
Heh... all this work just to avoid typing "SELECT * FROM ", which can be a really bad idea in itself depending on the number of rows in the table.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1490159
Posted Friday, August 30, 2013 7:45 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 13,139, Visits: 11,979
Jeff Moden (8/30/2013)
Heh... all this work just to avoid typing "SELECT * FROM ", which can be a really bad idea in itself depending on the number of rows in the table.


I agree. My guess is this has become one of those projects where you start out with a goal in mind and even though it gets more complicated than it seems you just "have" to finish it.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1490164
Posted Friday, August 30, 2013 7:55 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:58 PM
Points: 36,794, Visits: 31,253
Sean Lange (8/30/2013)
Jeff Moden (8/30/2013)
Heh... all this work just to avoid typing "SELECT * FROM ", which can be a really bad idea in itself depending on the number of rows in the table.


I agree. My guess is this has become one of those projects where you start out with a goal in mind and even though it gets more complicated than it seems you just "have" to finish it.


Agreed... it's a little like gambling... "Just one more turn of the wheel and I..."


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1490170
Posted Sunday, September 1, 2013 11:51 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 5:09 AM
Points: 45, Visits: 139
Hi,
this sp is helping me in development area in debugging the scripts to check the record count of a table on the spot. :)
Post #1490516
Posted Monday, September 2, 2013 12:11 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:12 AM
Points: 2,247, Visits: 2,708
There are better ways if all you need is the total record count of a table. Instead of returning all the data you could return just the number of rows (SELECT count(1) FROM table). This will prevent a lot of data being read and displayed on the screen.
Another way is to use the DMV's about the indexes. Read the rowcount from the HEAP (index_id = 0) or from the clustered index (index_id = 1). As far as I know this is the fastest way.
SELECT
object_name(object_id) as table_name
, row_count
FROM
sys.dm_db_partition_stats st
WHERE
index_id < 2
AND object_id = object_id('tablename')



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Post #1490518
Posted Monday, September 2, 2013 4:02 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 5:09 AM
Points: 45, Visits: 139
Sorry.. It was typo...

this sp is helping me in development area in debugging the scripts to see the data of a table on the spot. :)
Post #1490571
Posted Monday, September 2, 2013 7:30 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 7:16 AM
Points: 834, Visits: 2,695
saravanakumar.G (8/28/2013)
Hi,
I have tried and created one sp which will get the table name as input and will return all the records in that table. I will map this sp in the SSMS keyboard shortcut (just like Alt + F1 to return the table structure) so that i can use this shortcut to get the records from that table instead of writing some thing like SELECT * FROM tablename. this sp works fine and helped me a lot. however, if the table is belong to a schema then we have to pass the table name along with schema like schema.tablename. In this case, the sp is failing. If i put single quotes between the table name like 'schema.tablename' it is working fine. But is there any option to tweak the sp so that we dont have to pass the table name in quotes.

The sp is

create PROC [dbo].[sp_getrecords]
(
@TableName varchar(4000)

)
AS

exec('select * from '+@TableName+'')

You don't need a proc for this. If you map "SELECT * FROM " in the keyboard shortcut it will work as you want.




The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Post #1490619
Posted Monday, September 2, 2013 7:36 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 7:16 AM
Points: 834, Visits: 2,695




Excuse the type in the image, the comment should read -- Hit Ctrl+7




The SQL Guy @ blogspot

@SeanPearceSQL

About Me


  Post Attachments 
Test2.PNG (55 views, 36.11 KB)
Test1.PNG (55 views, 16.83 KB)
Post #1490623
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse