SQL stored procedure - sql

I have a table in sql in which I inserted my another table names.There are Table names column.So is it possible (programmatically inside SQL) getting the table names from this column and use them in Stored procedure as table name.How?inside SQL.
thanks

Sounds like something you will need to use dynamic SQL to do. Before you rush off to do this, I would recommend reading this article:
http://www.sommarskog.se/dynamic_sql.html

Related

How to get CREATE TABLE Statement from existing table in db2?

I am using DB2 and Oracle SQL Developer.
How to get the CREATE TABLE Statements from the existing tables?
There are too many tables and it will be a very lengthy process to do manually.
There is a special db2look utility for DDL extraction in Db2. You may refer to its options and their meaning at this link.
If you want SQL access to its capabilities, you may use the SYSPROC.DB2LK_GENERATE_DDL stored procedure supporting most of the utility's options. The routine has an output parameter getting "invocation number" int value after its call.
In case of a single table:
CALL SYSPROC.DB2LK_GENERATE_DDL ('-e -noview -t MY_SCHEMA.MY_TABLE', ?);
SELECT SQL_STMT
FROM SYSTOOLS.DB2LOOK_INFO_V
WHERE OP_TOKEN = <value_of_output_parameter_from_call_above>
ORDER BY OP_SEQUENCE;
In SQLDeveloper if you can see the table there's the initial Create Table Statement in the SQL Tab
You should do that for each table, this is a way to do it but I'm not sure it's fast enough for you.

Is it possible to add description for column while creating table in MSSQL QUERY

Is it possible to add description for column while creating table in MSSQL QUERY. I am in need to add description for columns in table while moving data to the table.
It's clunky in SQL Server. For some reason, they've never adopted the COMMENT syntax, and you can't add the comments directly in the CREATE TABLE statement.
After your CREATE statement, run the system stored procedure sp-addextendedproperty
There's an extended conversation on the topic under this question: SQL Comments on Create Table on SQL Server 2008

Insert record into a table which has 180 columns

I have 3 tables which have 180 columns, and I want to insert a record in all tables continuously using a stored procedure in SQL Server.
What is the best optimized way to do this?
Should I simply write an insert query in the stored procedure, or should I use functions for each insert query and after this I call function in my stored procedure?
Since functions in T-SQL cannot modify the database state, you cannot use them to insert data into a table. See the official MS documentation on function - scroll down to the section "Limitations and Restrictions" where it says:
User-defined functions cannot be used to perform actions that modify the database state.
Since you already have a stored procedure, you just need to write three separate INSERT statements (one for each table) inside that stored procedure to insert your data into the three tables by calling that stored procedure.
Does the 3 tables mentioned has the same number of column, as well as same column name?
If this is so, create a generic function with table name as input parameter would be more easy to use and maintain

Stored procedure for generic MERGE

I have a set of 10 tables in a database (DB1). And there are 10 tables in another database (DB2) with exact same schema on the same SQL Server 2008 R2 database server machine.
The 10 tables in DB1 are frequently updated with data.
I intend to write a stored procedure that would run once every day for synchronizing the 10 tables in DB1 with DB2. The stored procedure would make use of the MERGE statement.
Now, my aim is to make this as generic and parametrized as possible. That is, accommodate for more tables down the line... and accommodate different source and target DB names. Definitely no hard coding is intended.
This is my algorithm so far:
Have the database names as parameters
Have the first query within the stored procedure... result in giving the names of the 10 tables from a lookup table (this can be 10, 20 or whatever)
Have a generic MERGE statement that does the sync for each of the above set of tables (based on primary key?)
This is where I need more inputs on.
What is the best way to achieve this stored procedure? SQL syntax would be helpful.
I had to do something similar, to do that i used a string with a "skeleton" for the merge statement, then i retrieved the list of columns and the pks with a simple query on the sys views.
you could do something similar to build your merge statement, here's a sketch i wrote now as an example (I know it's horrible but i'm not going to write something decent at this hour, and it should give you a hint anyway :P )
SQLFiddle
then you just need to execute it with the usual sp_executesql stored procedure
by the way, always pay attention when building command strings this way, it's not that secure

MySQL Stored Procedure dynamic change name of table

I wanna change dynamicaly name of table in sql query. For example I have next stored procedure:
CREATE PROCEDURE NewProc(IN tableName varchar(64),IN message text)
BEGIN
INSERT INTO tableName VALUES (message);
END;
I need to change tableName in runtime, Can I to do it or not?
Thanks.
You must use dynamic SQL to prepare and execute an SQL string, to achieve what you describe.
Dynamic table names (or column names, or SQL keywords, etc.) must be interpolated into the SQL string before prepare. You can't use query parameters for these dynamic elements.
Be careful to avoid SQL injection vulnerabilities when you interpolate the table name into your SQL query. For example, you should check that the table name exists by looking it up in the information schema.
I agree with the comment from #OMG Ponies -- it's a code smell that you have multiple tables with identical structure such that you want to do the exact same insert to the exact same column. Code smells aren't a guarantee that you've got a bad design, but it's worth considering.