I want to pass a table name to a query
Declare @.Tablename '
Set @.TableName = 'MyTable'
Select * from @.TableName
how can i get this to work, if at all possibleResist the urge!
http://www.sommarskog.se/dynamic_sql.html
Keith
"Peter Newman" <PeterNewman@.discussions.microsoft.com> wrote in message
news:C461CC2C-7B1C-4606-A212-992D61F62B0A@.microsoft.com...
> I want to pass a table name to a query
> Declare @.Tablename '
> Set @.TableName = 'MyTable'
> Select * from @.TableName
> how can i get this to work, if at all possible|||You can use either a table variable or dynamic sql to work with dynamic tabl
e
names, a lot of experts will have tons of bad things to say about "dynamicly
"
creating tables but sometime you just can't help which I hope this is the
case otherwise you shouldn't use it :)
There are limitations to table variables which you can search for on the web
.
There are also limitations and nasties when you use dynamic SQL especially
in webased evironments where people can inject code into yours if they know
their way...
You can also have a look at local and global temp tables for the use of
adhoc tables .
Something that I can think of quickly
Table variable VS temp tables (# tables)
Table variables don't have schema, so you can't use indexing so massive
tables will be slow for querying, temp tables got a schema thus not a proble
m.
There are plenty of differences , pro's and cons for both.
What would be helpful to you is if you tell us what you are trying to
accomplish, then maybe we can be even more helpful.
Some code examples of "normal" table query using dynamic sql and table
variable.
HTH
declare @.mytable table (col1 int)
insert into @.mytable select '1'
select * from @.mytable
/**************/
create table mytable(col1 int)
insert into mytable select '1'
declare @.tablename nvarchar(22)
declare @.sql nvarchar(999)
set @.tablename = 'mytable'
set @.sql = 'select * from ' + @.tablename
exec sp_executesql @.sql
"Peter Newman" wrote:
> I want to pass a table name to a query
> Declare @.Tablename '
> Set @.TableName = 'MyTable'
> Select * from @.TableName
> how can i get this to work, if at all possible
Showing posts with label mytable. Show all posts
Showing posts with label mytable. Show all posts
Friday, March 23, 2012
is this Possible
Monday, February 20, 2012
Is there an MS SQL Limit function?
MySQL has a convenient syntax for paging data that looks like this:
SELECT * FROM MyTable LIMIT 10, 20
That would select 10 records, starting from record 20, so that it returns records 20 - 30. This is convenient way to page data, without returning anymore rows than than you need.
However, MS SQL doesn't appear to support that syntax. What is the equivalent sql code to select any N rows from an arbitrary starting point, without having to create a stored procedure?
Thanks in advance :)it's a fiasco
SQL Server has the TOP keyword, but it takes only one parameter
see this article -- http://rosca.net/writing/articles/serverside_paging.asp|||How can is start at row 20 when you have not specified an ORDER BY clause?|||How can is start at row 20 when you have not specified an ORDER BY clause?
You can't
Read here
http://weblogs.sqlteam.com/jeffs/category/162.aspx|||http://weblogs.sqlteam.com/jeffs/category/162.aspxjeepers, i took a look at one of the two articles posted there, and boy, that sql is inefficient
brett, did you read the article i posted?|||Which one? I thought the server side paging was pretty good...|||Andrew's code is very,,,need to compare the 2|||i originally read the second one, and it has issues
i just now went back and read the first one, and all it is is a dynamic-ization of the second one
i remain unimpressed
now, did you read the article i posted?|||Yes I did, and it's elegant...but I'd have to test it for performace against some major tables
SELECT * FROM MyTable LIMIT 10, 20
That would select 10 records, starting from record 20, so that it returns records 20 - 30. This is convenient way to page data, without returning anymore rows than than you need.
However, MS SQL doesn't appear to support that syntax. What is the equivalent sql code to select any N rows from an arbitrary starting point, without having to create a stored procedure?
Thanks in advance :)it's a fiasco
SQL Server has the TOP keyword, but it takes only one parameter
see this article -- http://rosca.net/writing/articles/serverside_paging.asp|||How can is start at row 20 when you have not specified an ORDER BY clause?|||How can is start at row 20 when you have not specified an ORDER BY clause?
You can't
Read here
http://weblogs.sqlteam.com/jeffs/category/162.aspx|||http://weblogs.sqlteam.com/jeffs/category/162.aspxjeepers, i took a look at one of the two articles posted there, and boy, that sql is inefficient
brett, did you read the article i posted?|||Which one? I thought the server side paging was pretty good...|||Andrew's code is very,,,need to compare the 2|||i originally read the second one, and it has issues
i just now went back and read the first one, and all it is is a dynamic-ization of the second one
i remain unimpressed
now, did you read the article i posted?|||Yes I did, and it's elegant...but I'd have to test it for performace against some major tables
Subscribe to:
Posts (Atom)