473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 1808
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
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...
7
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...
4
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...
13
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...
3
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...
3
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....
5
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...
1
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...
2
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.