[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

Dynamically create table name in TSQL

bubbles

3/30/2007 5:29:00 AM

Newbie using SQL Server 2005 Enterprise:


I need to dynamically create a TABLE NAME based on a variable
@PRODUCT,
but cannot seem to get it right. The table name has to be inserted
into the TSQL
statement location <Here_Is_My_Problem>



DECLARE @PRODUCT NVARCHAR(35)
DECLARE @NUM_ROWS INTEGER

DECLARE PRODUCTS CURSOR
FOR SELECT ProdName FROM dbo.PRODUCTS

SET @NUM_ROWS = 0

OPEN PRODUCTS

WHILE @NUM_ROWS < @@CURSOR_ROWS
BEGIN
FETCH PRODUCTS INTO @PRODUCT
SET @NUM_ROWS = @NUM_ROWS + 1

/* Create a new table based on the variable @PRODUCT */
SELECT * INTO <Here_Is_My_Problem> FROM dbo.ANOTHER_TABLE
WHERE ProdName = @PRODUCT
END


Thanks,
Bubbles

13 Answers

xyb

3/30/2007 6:10:00 AM

0

On 3?30?, ??1?28?, "bubbles" <bubbles....@hotmail.com> wrote:
> Newbie using SQL Server 2005 Enterprise:
>
> I need to dynamically create a TABLE NAME based on a variable
> @PRODUCT,
> but cannot seem to get it right. The table name has to be inserted
> into the TSQL
> statement location <Here_Is_My_Problem>
>
> DECLARE @PRODUCT NVARCHAR(35)
> DECLARE @NUM_ROWS INTEGER
>
> DECLARE PRODUCTS CURSOR
> FOR SELECT ProdName FROM dbo.PRODUCTS
>
> SET @NUM_ROWS = 0
>
> OPEN PRODUCTS
>
> WHILE @NUM_ROWS < @@CURSOR_ROWS
> BEGIN
> FETCH PRODUCTS INTO @PRODUCT
> SET @NUM_ROWS = @NUM_ROWS + 1
>
> /* Create a new table based on the variable @PRODUCT */
> SELECT * INTO <Here_Is_My_Problem> FROM dbo.ANOTHER_TABLE
> WHERE ProdName = @PRODUCT
> END
>
> Thanks,
> Bubbles
so what is your problem?
such code may help you
declare @tablename varchar(100)
select @tablename = 'test'
--can not use tablename from a variable
--select top 100 * into @tablename from sysobjects
--select * from @tablename

if you realy want to do this trick,use dynamic sql.

bubbles

3/30/2007 6:35:00 AM

0

There are many products, and new products will be added.
The application must be able to dynamically create the table names.

In other applications, I can create an SQL statement before running
it, so that is not a problem.

I just don't know how to construct a TSQL statement with a variable
name as the table name.

BTW, what is dynamic SQL?

Thanks,
Bubbles



On Mar 30, 2:10 pm, "xyb" <xiangyua...@gmail.com> wrote:
>
> so what is your problem?
> such code may help you
> declare @tablename varchar(100)
> select @tablename = 'test'
> --can not use tablename from a variable
> --select top 100 * into @tablename from sysobjects
> --select * from @tablename
>
> if you realy want to do this trick,use dynamic sql.- Hide quoted text -
>
> - Show quoted text -


xyb

3/30/2007 6:40:00 AM

0

On 3?30?, ??2?34?, "bubbles" <bubbles....@hotmail.com> wrote:
> There are many products, and new products will be added.
> The application must be able to dynamically create the table names.
>
> In other applications, I can create an SQL statement before running
> it, so that is not a problem.
>
> I just don't know how to construct a TSQL statement with a variable
> name as the table name.
>
> BTW, what is dynamic SQL?
>
> Thanks,
> Bubbles
>
> On Mar 30, 2:10 pm, "xyb" <xiangyua...@gmail.com> wrote:
>
>
>
>
>
> > so what is your problem?
> > such code may help you
> > declare @tablename varchar(100)
> > select @tablename = 'test'
> > --can not use tablename from a variable
> > --select top 100 * into @tablename from sysobjects
> > --select * from @tablename
>
> > if you realy want to do this trick,use dynamic sql.- Hide quoted text -
>
> > - Show quoted text -- ??????? -
>
> - ??????? -

http://www.sommarskog.se/dynami...

bubbles

3/30/2007 6:45:00 AM

0

Thank you!
Xie xie ni !


On Mar 30, 2:39 pm, "xyb" <xiangyua...@gmail.com> wrote:
>
> http://www.sommarskog.se/dynamic... Hide quoted text -
>
> - Show quoted text -


bubbles

3/30/2007 9:37:00 AM

0

Thank you very much! Problem solved as follows:



DECLARE @PRODUCT NVARCHAR(50),
@TABLE_NAME NVARCHAR(50),
@SQL NVARCHAR(MAX),
@NUM_ROWS INT

DECLARE PRODUCT_LIST CURSOR
FOR SELECT ProdName, ModifiedProdName AS TName FROM ProductList

SET @NUM_ROWS = 0

OPEN PRODUCT_LIST

WHILE @NUM_ROWS < @@CURSOR_ROWS
BEGIN
FETCH PRODUCT_LIST INTO @PRODUCT, @TABLE_NAME

/* Drop old data table */
SET @SQL = 'DROP TABLE ' + @TABLE_NAME
EXEC(@SQL)

/* Generate new table */
SET @SQL = "SELECT * INTO ' + @TABLE_NAME + ' FROM Products WHERE
ProdName = ''' + @PRODUCT + ''' ORDER BY Col1, Col2'
EXEC(@SQL)

/* Increment counter */
SET @NUM_ROWS = @NUM_ROWS + 1
END

CLOSE PRODUCT_LIST
DEALLOCATE PRODUCT_LIST


Works beautifully.


THANK YOU!!!

Bubbles







On Mar 30, 2:39 pm, "xyb" <xiangyua...@gmail.com> wrote:
>
> http://www.sommarskog.se/dynamic... Hide quoted text -
>
> - Show quoted text -


--CELKO--

3/30/2007 5:23:00 PM

0

>> I need to dynamically create a TABLE NAME based on a variable
@PRODUCT, but cannot seem to get it right. <<

Your whole approach is totally wrong. In RDBMS, we start with a data
model based on some real world (get a basic book on the topic) and
then create tables that reflect that reality. A table can be a set of
a particular kind of entities or a particular relationship. Notice
the word "particular" -- that means all tables are known and named.

Theya re not created on the fly -- that would be a magical world where
elephants drop out of the sky.

The rest of your is nothing a magnetic tape file system mimicked in
SQL, not an RDBMS at all. You are trying to hang a scratch tape, add
PHYSICAL row numbers and spool the "records" (huge difference between
rows and records! Learn it), so you can use the "scratch tape" in
another procedural process.

Thinking is sets, and not tapes, create a VIEW, which is a virtual
table (no such thing as a "virtual mag tape!"m is there?).

CREATE VIEW ProductCounts
AS product_id, product_cnt
SELECT product_id, COUNT(*)
FROM Products
GROUP BY product_id;

The loop and the row numbering (i.e. magnetic tape record numbers
mimicked in SQL) were useless; you can use the actual count in one
aggregation instead.

Now simply write:

SELECTproduct_id, product_cnt
FROM ProductCounts
WHERE product_id = @my_product_id;

Steve Dassin

3/30/2007 7:52:00 PM

0

"--CELKO--" <jcelko212@earthlink.net> wrote in message
news:1175275398.409658.294160@p15g2000hsd.googlegroups.com...
> Theya re not created on the fly -- that would be a magical world where
> elephants drop out of the sky.

Precisely what Dick Cheney thought on Nov, 6. On Nov 8 do you think
his view of magic changed? Magic is the state of denial for true believers.
And no one gives denial as well as you do -:)


Erland Sommarskog

4/1/2007 7:27:00 PM

0

bubbles (bubbles.one@hotmail.com) writes:
> Thank you very much! Problem solved as follows:
>
>
>
> DECLARE @PRODUCT NVARCHAR(50),
> @TABLE_NAME NVARCHAR(50),
> @SQL NVARCHAR(MAX),
> @NUM_ROWS INT
>
> DECLARE PRODUCT_LIST CURSOR
> FOR SELECT ProdName, ModifiedProdName AS TName FROM ProductList
>
> SET @NUM_ROWS = 0
>
> OPEN PRODUCT_LIST
>
> WHILE @NUM_ROWS < @@CURSOR_ROWS
> BEGIN
> FETCH PRODUCT_LIST INTO @PRODUCT, @TABLE_NAME
>
> /* Drop old data table */
> SET @SQL = 'DROP TABLE ' + @TABLE_NAME
> EXEC(@SQL)
>
> /* Generate new table */
> SET @SQL = "SELECT * INTO ' + @TABLE_NAME + ' FROM Products WHERE
> ProdName = ''' + @PRODUCT + ''' ORDER BY Col1, Col2'
> EXEC(@SQL)
>
> /* Increment counter */
> SET @NUM_ROWS = @NUM_ROWS + 1
> END
>
> CLOSE PRODUCT_LIST
> DEALLOCATE PRODUCT_LIST
>
>
> Works beautifully.

I still don't know exactly what you are doing, but you are most probably
not doing the right thing. Just put all data into one table, make your
computations. If you want the users to see these as different tables in
the Access interfaces, define views in Access to handle this.

Again and again: SQL is completely different mindset from VBA and similar
style of programming.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/...
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/...

Eric Ramon

11/2/2011 9:38:00 PM

0

On Nov 1, 8:03 pm, "who?" <yourimageunre...@sbcglobal.net> wrote:
> On Nov 1, 9:43 pm, "RichL" <rpleav...@yahoo.com> wrote:
>
>
> > EVERY post in this thread that supposedly came from "Frank" is a forge (as
> > if him talking about feet wasn't enough of a clue).
>
> Rich, he's been accused of talking about "feet" before. I clicked on
> on
> his other email address as well as this one. It appeared to be Frank.
> He contacted me through youtube a couple of days ago with his
> yahoo address that matched this one.
>

I think Rich is right. It's an old regular sending these out.

Jeff

11/2/2011 10:43:00 PM

0

On Nov 2, 4:38 pm, Eric Ramon <ramon.e...@gmail.com> wrote:
> On Nov 1, 8:03 pm, "who?" <yourimageunre...@sbcglobal.net> wrote:
>
> > On Nov 1, 9:43 pm, "RichL" <rpleav...@yahoo.com> wrote:
>
> > > EVERY post in this thread that supposedly came from "Frank" is a forge (as
> > > if him talking about feet wasn't enough of a clue).
>
> > Rich, he's been accused of talking about "feet" before. I clicked on
> > on
> > his other email address as well as this one. It appeared to be Frank.
> > He contacted me through youtube a couple of days ago with his
> > yahoo address that matched this one.
>
> I think Rich is right. It's an old regular sending these out.

I think it's an old regular who appears in some ways to
not have posted here close to a year...but who actually
never left here....who is doing this. They are my main
suspect. The motive? I would say that for whatever
reason they identify with certain aspects of Frank's
personality relating to their own life. They hurt very
much inside, so they are projecting their own
insecurities onto him to deal with their own pain.