473,789 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using dynamic sql to write a procedure for table creation

hi friends,
i want to create a procedure like when i give a name as parameter to
the procedure it should create a table with that name and with two
column names as any type. plz explain it with dynamic sql.

Nov 12 '05 #1
7 1851
narayana wrote:
hi friends,
i want to create a procedure like when i give a name as parameter to
the procedure it should create a table with that name and with two
column names as any type. plz explain it with dynamic sql.


Construct a string (VARCHAR) that contains the CREATE TABLE statement and
then call EXECUTE IMMEDIATE.

CREATE PROCEDURE create_tab ( tab IN VARCHAR(128) )
BEGIN
DECLARE stmt VARCHAR(1000);
SET stmt = 'CREATE TABLE ' || tab ||
' ( id INT NOT NULL PRIMARY KEY )';
EXECUTE IMMEDIATE stmt;
END@

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 12 '05 #2
narayana wrote:
hi friends,
i want to create a procedure like when i give a name as parameter to
the procedure it should create a table with that name and with two
column names as any type. plz explain it with dynamic sql.

Google for my name and PREPARE or EXECUTE I have posted more examples
that I remember here....

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #3
Why are you doing this?? One of the most basic ideas of RDBMS is that
you have a data model of a real world which is represented by tables in
a schema. To create tables with procedures on the fly woudl be like
having elephants drop out of the sky.

These magical tables clog up the schema and destroy the data model. ,
area screaming pain to maintain, etc.

Nov 12 '05 #4
no not that ,
when the situation demands with in the application programs then what
is the way .

According to the basics, dml statements are not recommended in
procedures.

then there is only one way that is dynamic sql with dml execution in
procedure

are you agreee ?

Nov 12 '05 #5
NO. Application code does not drive the data model. The data model
comes frist and then the application is built on the model. You should
know all the tables in the schema when you start.

Newbies will mimic scratch files with tables that are created dynamic
SQL because they are used to coding file systems that work that way.

Nov 12 '05 #6
The requirements come first, the specifications second. The
requirements pretty much define the application while the specification
defines the data model.

A business does not care how the data is stored, so long as the
application can use it. In fact, the data is stored most efficiently to
be accessed by the program. Warehouse or OLTP is defined by the
application, the database simply implements it. If the application
needs a query, the database makes it work.

The data model does, however, define efficiencies, security, and
relationships. In these areas, the database comes first and the
application second (well, in theory at least).

---

While i agree that it is rare that an application should do DDL, it is
not taboo. There are times when it is the best way to do things. For
example, if a file is to be imported for known columns, but the order
of the columns in the file is not known, an application can CREATE a
TABLE on the fly to hold the imported data in original form and then
have a PROCEDURE access the appropriate data.

Or, for backup purposes, having a monthly process CREATE a history
TABLE with the year and month included in the TABLE's name, moving the
historical data to it, and finally archiving it.

--

If you really want to help, you should first answer the question(s),
and then mention that it is unlikely that an application should do DDL,
both from an efficiency and from a data design standpoint. But to say
"Why are you doing this??" or scream out "NO." and then give your own
personal beliefs on design theory is pedantic and unwelcome.

B.

Nov 12 '05 #7
>> While i agree that it is rare that an application should do DDL, it is not taboo.<<

Yes, it is. This is foundations, not a matter of personal style.
For example, if a file is to be imported for known columns, but the order of the columns in the file is not known, an application can CREATE a TABLE on the fly to hold the imported data in original form and then have a PROCEDURE access the appropriate data. <<
I would think that you might want to use an ETL tool for that kind of
thing. And that would be done **outside** of the schema. Ordering is
a physical problem, not a data model or logical problem. Likewise,
converting units from SI to English, Dewey Decimal to LOC, etc. are
physical format problems. Why do you think otherwise?
If you really want to help, you should first answer the question(s), and then mention that it is unlikely that an application should do DDL, both from an efficiency and from a data design standpoint. <<


After teaching SQL for 20+ years and writing six books on SQL, I
disagree. When someone has missed the principles, lecture them about
those principles. Most of the time, ifyou can see what their mental
model was and why they made an error, then you can REALLY help them.
There is a literature on "How we know what is not so" in critical
thinking. Most sane people work from a metnal model and do not behave
at random.

I then suggest a better answer, based on the principles. My "Zen
Master with a stick approach" bothers the current generation of whining
kids with a sense of entitlement. But I have warched this lack of
professionalism kill people (Google some of my posting about a flawed
African medical relief database on other newsgroups; the database was
designed for the front end form screens wthout a real data model).

Bad programmers kill people.

If you give someone a Kludge first, that is all they will hear and that
is what they will do from that time forward. If this was a fine
furniture newsgroup and someone asked for the best kind of rocks to use
for pounding screws into wood, you can answer "Granite!! Bigger chunks
work faster!" or you can answer "NO! Let me tell you about screws.
They can be more complicated than rocks, but they are worth the effort
and the learning curve. etc."

Nov 12 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1993
by: Golawala, Moiz M (GE Infrastructure) | last post by:
Hi All, I am having problem returning values from a Stored Procedure that creates a dynamic table (table variable) inserts values during a procedure and then I select from that dynamic table to furnish values to python. This does not work MY PYTHON CODE IS: import adodbapi connStrSQLServer = r"Provider=SQLOLEDB.1; User ID=sa; Password=tester; Initial Catalog=someDB;Data Source=someSource" db = adodbapi.connect(connStrSQLServer)
7
10240
by: Alex Vorobiev | last post by:
hi there, i am using sql server 7. below is the stored procedure that is giving me grief. its purpose it two-fold, depending on how it is called: either to return a pageset (based on page number and page size), or to return IDs of previous and next records (based on current record id). the problem is, that the order in which records are inserted into the temp table is inconsistent, even though the calling statement and the order by is...
4
13470
by: marc | last post by:
I've been developing a stored procedure that uses a user defined function in the query portion of the procedure. However, since the end product needs to allow for dynamic table names, the UDF will not work. I've been trying to get this to work with converting the UDF to a procedure, but I'm having no luck. Here is the background on what I'm trying to accomplish. I need to perform a sub-identity on a table, I have the normal identity set,...
13
9651
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
3
24037
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I tried to update the links by changing the Connect property and refreshing: Set td = db.TableDefs(0)...
3
2942
by: CAD Fiend | last post by:
Hello, Well, after an initial review of my database by my client, they have completely changed their minds about how they want their form. As a result, I'm having to re-think the whole process. My Current Form (6 tabs): - Owner, Property, Title, Docs, Queries, & Reports - User is able to see (while navigating through the tabs) above in the form : Owner Name, Address, Parcel, and SSN
5
1109
by: Ronald S. Cook | last post by:
I've read a few posts on the stored procedure vs dynamic sql debate. I ran a few performance test for myself and it appears to be a wash. Given that, I'm leaning toward dynamic sql mostly because it would mean one fewer place to have things. But, before we go that route we wanted to ask the question: Is there any compelling reason why we shouldn't abandon all of our stored procs and just write the SQL inside inside our functions in...
1
5234
by: hello2008 | last post by:
Hi, I have just started coding in PHP. I have coded a web page using HTML, JS, and PHP. An HTML table has to be populated dynamically using the data from the backend. Presently I have 5 records in the backend table so I get 5 HTML-table rows. I have created a session object starting from the login page. The requirement is that as soon as I log in and my request gets forwarded to the foll PHP page I should be seeing the foll. dynamically...
2
5655
by: IuliaS | last post by:
Hello everyone! I want to create a stored procedure, so I can more easily, and transparent retrieve data from db2. Long story short: when a user wants to put some data in the DB, he also creates the tables and their links. When getting the data from the DB... well suffice to say it's ugly. I want to use one stored procedure that will return a result set as (name, value) pairs so I can display it nice and easy in the UI. So far I've managed to...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.