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


sp_getrecords to get all records from a table


sp_getrecords to get all records from a table

Author
Message
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84589 Visits: 41067
saravanakumar.G (8/28/2013)
any help on this??? Sad


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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
saravanakumar.G
saravanakumar.G
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 215
Thanks for all of your reply Smile

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.. Smile
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84589 Visits: 41067
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25807 Visits: 17509
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. Hehe

_______________________________________________________________

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 Modens 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)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84589 Visits: 41067
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. Hehe


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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
saravanakumar.G
saravanakumar.G
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 215
Hi,
this sp is helping me in development area in debugging the scripts to check the record count of a table on the spot. Smile
HanShi
HanShi
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4359 Visits: 3668
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’! **
saravanakumar.G
saravanakumar.G
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 215
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. Smile
Sean Pearce
Sean Pearce
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1758 Visits: 3432
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
Sean Pearce
Sean Pearce
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1758 Visits: 3432




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



The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Attachments
Test2.PNG (59 views, 36.00 KB)
Test1.PNG (59 views, 16.00 KB)
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