|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
About Cursor on Dynamically created Tables.SET @SID = '00260AMIT' DECLARE @STR VARCHAR(1000) SET @STR = '' SET @STR = 'CREATE TABLE RD_'+ @SID +' (RESPID INT PRIMARY KEY, FLAG BIT)' EXEC(@STR) DECLARE @FIELDCODE INT, @FIELDNAME VARCHAR(20) SET @STR = 'DECLARE CURTABLECREATION CURSOR LOCAL STATIC FORWARD_ONLY FOR SELECT DISTINCT CODE, DESCRIPT FROM UI_'+ @SID +' ORDER BY CODE OPEN CURTABLECREATION FETCH NEXT FROM CURTABLECREATION INTO @FIELDCODE, @FIELDNAME WHILE @@FETCH_STATUS = 0 BEGIN SET @STR = ''ALTER TABLE RD_'+ @SurveyID +' ADD ' + @FIELDNAME + ' TINYINT'' EXEC(@STR) FETCH NEXT FROM CURTABLECREATION INTO @FIELDCODE, @FIELDNAME END CLOSE CURTABLECREATION DEALLOCATE CURTABLECREATION' I want to run this cursor. This is based on the table UI_00260AMIT and rd_00260AMIT. These tables are created just before this step. In fact you can see the code of RD_00260AMIT creation. Now my questions is how do I run a cursor that is based on the table that created dynamically. please suggest. Perhaps if you explain what it is that you're actually trying to acomplish we
can provide you with a solution that doesn't need dynamically created tables with dynamically added columns at all. ML --- http://milambda.blogspot.com/ Actaully I want to create a table whose columns are depends upon the
record of other table. and also i don't know the name of table in advance. it will create dynamically. ML wrote: Show quote > Perhaps if you explain what it is that you're actually trying to acomplish we > can provide you with a solution that doesn't need dynamically created tables > with dynamically added columns at all. > > > ML > > --- > http://milambda.blogspot.com/ Could you give an example? Why do you choose to create SQL objects dynamically?
ML --- http://milambda.blogspot.com/ amit (amitsya***@gmail.com) writes:
> I want to run this cursor. This is based on the table UI_00260AMIT and If you are creating tables dynamically, there is probably something > rd_00260AMIT. These tables are created just before this step. In fact > you can see the code of RD_00260AMIT creation. Now my questions is how > do I run a cursor that is based on the table that created dynamically. fundamentally wrong in your database design. In a relational database, the schema is supposed to be static, and only change when you install a new version of the product. The operations in SQL reflects this, by working static column and table names only. As for your question, it appears that the outer @STR that you assign would be executable, if you only add a declaration of the inner @STR. Keep in mind that the code executed through EXEC() constitutes a scope of its own. But normally, it's sufficient to excecute the DECLARE CURSOR statement through EXEC. However, if you do this, you cannot use the LOCAL keyword, as the cursor must be visible for the outer scope too. > Actaully I want to create a table whose columns are depends upon the If you tells of the actual business problem, we might be able to> record of other table. and also i don't know the name of table in > advance. it will create dynamically. suggest a better solutoin. -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx amit wrote:
Show quote > DECLARE @SID Varchar(10) My suggestion is that you scrap the whole idea and think again. Your> SET @SID = '00260AMIT' > DECLARE @STR VARCHAR(1000) > SET @STR = '' > > SET @STR = 'CREATE TABLE RD_'+ @SID +' (RESPID INT PRIMARY KEY, FLAG > BIT)' > EXEC(@STR) > > DECLARE @FIELDCODE INT, @FIELDNAME VARCHAR(20) > SET @STR = 'DECLARE CURTABLECREATION CURSOR LOCAL STATIC FORWARD_ONLY > FOR > SELECT DISTINCT CODE, DESCRIPT FROM UI_'+ @SID +' ORDER BY CODE > OPEN CURTABLECREATION > FETCH NEXT FROM CURTABLECREATION INTO @FIELDCODE, @FIELDNAME > WHILE @@FETCH_STATUS = 0 > BEGIN > SET @STR = ''ALTER TABLE RD_'+ @SurveyID +' ADD ' + @FIELDNAME + ' > TINYINT'' > EXEC(@STR) > FETCH NEXT FROM CURTABLECREATION INTO @FIELDCODE, @FIELDNAME > END > CLOSE CURTABLECREATION > DEALLOCATE CURTABLECREATION' > > > I want to run this cursor. This is based on the table UI_00260AMIT and > rd_00260AMIT. These tables are created just before this step. In fact > you can see the code of RD_00260AMIT creation. Now my questions is how > do I run a cursor that is based on the table that created dynamically. > > please suggest. proposed scheme is unsound and highly impractical. Possibly someone here could offer some advice if you explain more about the application. Unfortunately it's very hard and often unwise to give detailed design advice online. The limited information we can exchange in an online conversation makes it much easier for us to criticize a bad solution than to design good one. If you don't have a lot of experience at database design then you should consider hiring some expertise to help out with your project. -- David Portas, SQL Server MVP Whenever possible please post enough code to reproduce your problem. Including CREATE TABLE and INSERT statements usually helps. State what version of SQL Server you are using and specify the content of any error messages. SQL Server Books Online: http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx -- David,
Thanks for your advice. I have already done this. Actually here I have to run an application on a global user account. and i have written some code that creates some tables, but if i run this with a single user account then my tables get overwritten by one another coz this application will run by more then one user simultaneously. So for this purpose i have to write the script in such format that it creates unique tables and perform the execution successfully. coz first i have upload data from excel then run sp to perform my logic and expost the final data in excel format. I think by this you can understand why did i choose this method of creating tables runtime. thanks. David Portas wrote: Show quote > amit wrote: > > DECLARE @SID Varchar(10) > > SET @SID = '00260AMIT' > > DECLARE @STR VARCHAR(1000) > > SET @STR = '' > > > > SET @STR = 'CREATE TABLE RD_'+ @SID +' (RESPID INT PRIMARY KEY, FLAG > > BIT)' > > EXEC(@STR) > > > > DECLARE @FIELDCODE INT, @FIELDNAME VARCHAR(20) > > SET @STR = 'DECLARE CURTABLECREATION CURSOR LOCAL STATIC FORWARD_ONLY > > FOR > > SELECT DISTINCT CODE, DESCRIPT FROM UI_'+ @SID +' ORDER BY CODE > > OPEN CURTABLECREATION > > FETCH NEXT FROM CURTABLECREATION INTO @FIELDCODE, @FIELDNAME > > WHILE @@FETCH_STATUS = 0 > > BEGIN > > SET @STR = ''ALTER TABLE RD_'+ @SurveyID +' ADD ' + @FIELDNAME + ' > > TINYINT'' > > EXEC(@STR) > > FETCH NEXT FROM CURTABLECREATION INTO @FIELDCODE, @FIELDNAME > > END > > CLOSE CURTABLECREATION > > DEALLOCATE CURTABLECREATION' > > > > > > I want to run this cursor. This is based on the table UI_00260AMIT and > > rd_00260AMIT. These tables are created just before this step. In fact > > you can see the code of RD_00260AMIT creation. Now my questions is how > > do I run a cursor that is based on the table that created dynamically. > > > > please suggest. > > My suggestion is that you scrap the whole idea and think again. Your > proposed scheme is unsound and highly impractical. > > Possibly someone here could offer some advice if you explain more about > the application. Unfortunately it's very hard and often unwise to give > detailed design advice online. The limited information we can exchange > in an online conversation makes it much easier for us to criticize a > bad solution than to design good one. If you don't have a lot of > experience at database design then you should consider hiring some > expertise to help out with your project. > > -- > David Portas, SQL Server MVP > > Whenever possible please post enough code to reproduce your problem. > Including CREATE TABLE and INSERT statements usually helps. > State what version of SQL Server you are using and specify the content > of any error messages. > > SQL Server Books Online: > http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx > -- amit (amitsya***@gmail.com) writes:
> Thanks for your advice. I have already done this. Or there is a permanent table, with a key identifying the current user,> Actually here I have to run an application on a global user account. > and i have written some code that creates some tables, but if i run > this with a single user account then my tables get overwritten by one > another coz this application will run by more then one user > simultaneously. So for this purpose i have to write the script in such > format that it creates unique tables and perform the execution > successfully. coz first i have upload data from excel then run sp to > perform my logic and expost the final data in excel format. > I think by this you can understand why did i choose this method of > creating tables runtime. so that you can handle loads from simultaneous users. What you should use for key, depends on how the load process looks like. In the simplest case you can use @@spid, but I suspect that will not fly here, because you are likely to have one more connection. Maybe host_name() would do. -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx amit wrote:
Show quote > David, No I cannot.> > Thanks for your advice. I have already done this. > Actually here I have to run an application on a global user account. > and i have written some code that creates some tables, but if i run > this with a single user account then my tables get overwritten by one > another coz this application will run by more then one user > simultaneously. So for this purpose i have to write the script in such > format that it creates unique tables and perform the execution > successfully. coz first i have upload data from excel then run sp to > perform my logic and expost the final data in excel format. > I think by this you can understand why did i choose this method of > creating tables runtime. Do you still have a question or was it answered? -- David Portas, SQL Server MVP Whenever possible please post enough code to reproduce your problem. Including CREATE TABLE and INSERT statements usually helps. State what version of SQL Server you are using and specify the content of any error messages. SQL Server Books Online: http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx -- |
|||||||||||||||||||||||