Stored procedure does not create new table - asp.net

I am trying to create tables dynamically in a SQL Server database.
Like this with input from a textbox:
ALTER PROCEDURE [dbo].[opretNyEsyn]
#Navn NVARCHAR(100)
AS
BEGIN
DECLARE #SQLString NVARCHAR(MAX)
SET #SQLString = 'create table ' + QUOTENAME(#Navn) +
'([EsynNummer][int]Identity(1,1),
[Dato][datetime])'
END
But nothing happens when I run the method from the form, the table isn't created, and I get no errors.
What am I missing?
Notice. This is just a test table. It isn't supposed to look like this in the end.
Thanks in advance

You are just creating the statement, but in order to create the tables, you need to Execute the statement using the EXEC statement of sp_executesql SP. Add an execute statement to your Procedure. Like this
ALTER PROCEDURE [dbo].[opretNyEsyn]
#Navn nvarchar(100)
as
Begin
declare #SQLString nvarchar(max)
set #SQLString = 'create table ' + QUOTENAME(#Navn)+
'([EsynNummer][int]Identity(1,1),
[Dato][datetime])'
exec(#SQLString)-- Execute the Statement
End
Go

You need to execute dynamic SQL:
ALTER PROCEDURE [dbo].[opretNyEsyn]
#Navn SYSNAME
as
Begin
declare #SQLString nvarchar(max);
set #SQLString = 'create table ' + QUOTENAME(#Navn)+
'([EsynNummer][int]Identity(1,1),
[Dato][datetime])';
EXECUTE (#SQLString);
End;
Anyway I recommend to read The Curse and Blessings of Dynamic SQL and Packaging Permissions in Stored Procedures:
CREATE TABLE #tbl
The desire here is to create a table of which the name is determined at run-time.
If we just look at the arguments against using dynamic SQL in stored procedures, few of them are really applicable here. If a stored procedure has a static CREATE TABLE in it, the user who runs the procedure must have permissions to create tables, so dynamic SQL will not change anything. Plan caching obviously has nothing to do with it. Etc.
Nevertheless: Why? Why would you want to do this? If you are creating tables on the fly in your application, you have missed some fundamentals about database design. In a relational database, the set of tables and columns are supposed to be constant. They may change with the installation of new versions, but not during run-time.

Related

Can't store other language data in nvarchar column

I am using SQL Server 2008.
This is my Table definition.
create table myTable (nid int identity(1,1),content nvarchar(max));
when I execute these two queries
insert into myTable(content)values(N'हिंदी में परीक्षण');
update myTable set content=N'हिंदी में परीक्षण' where nid=1;
the other language data is inserted/updated in the table. I want to do the same in a stored procedure. This is the definition of my stored procedure.
create proc myProc
#nid int,
#content nvarchar(max)
as
begin
update myTable set content=#content where nid=#nid;
end
it just doesn't work the column is updated with ????????????? value. What should I do. I tried to do this
update myTable set content=N''+#content where nid=#nid;
But this doesn't work as well. Please Help.
I forgot to tell one thing
exec myProc 1,N'हिंदी में परीक्षण'
this does work of course it does the problem is i send data from an asp .net web application where i use this syntax.
public void myProcCall()
{
dbcommand = db.GetStoredProcCommand("myProc", nid, content);
ds = db.ExecuteDataSet(dbcommand);
}
So when I send other language content from the web application the value is updated as ??????????. Do I have to make some changes in my asp .net code.
This will work
EXEC myProc 1, N'हिं में पदी रीक्षण'
This won't
EXEC myProc 1, 'हिं में पदी रीक्षण'
Ergo, the parameter is sent as non-unicode
You need to ensure that the code that populate the parameter sends unicode

TERADATA SQL: Populate one table through query results stored in another table

TERADATA SQL, I have SQL statements stored in a table. I want to populate another table with results of those SQL statements. SQL statements are different for each row and each column. Could you please tell me how I can achieve it? I have tried to get results through Stored Procedure and Shell Script. However, I couldn't code it properly.
Here's a pretty barebones procedure for pulling something like this off. It gets a little hairy because:
We are employing a cursor to loop through your source table that holds the SQL Statements
We are employing a second cursor while looping through the first cursor to dynamically execute the SQL String and FETCH the results of that SQL query into a variable.
I would suspect you will run into all sorts of bugs while getting this set up, but I believe this is pretty close to a working procedure:
CREATE PROCEDURE <yourdatbase>.MyNewProcedure()
BEGIN
--Declare variables to hold the three fields as we loop through them with the cursor
--You'll need to change these types to match your table's data type so we can fetch
-- them without error inside the cursor loop
DECLARE customerID INT;
DECLARE customerName VARCHAR(50);
DECLARE sqlQUERY as VARCHAR(10000);
DECLARE source_table_cursor CURSOR FOR SELECT customerID, customerName, SQLQuery FROM <yourdatabase>.yoursourcetable FOR READ ONLY;
OPEN source_table_cursor;
looplabel:
LOOP
--We'll need a cursor for a dynamic statement
--And we'll also need an integer to catch your query
--results (Assuming they are all like (Sum(<somefield>) or some
--type of INT
DECLARE C1 CURSOR FOR S1;
DECLARE sqlResults as INT;
FETCH source_table_cursor INTO customerID, customerName, sqlQuery;
--WOAH THERE! Cursor issues something went wrong die die die
IF (SQLSTATE ='02000') THEN
LEAVE label1;
END IF;
--Now that we have a record and a sql statement we will need another cursor
--where we will execute, dynamically, the SQL statement and then fetch the
--single record result to update our target table
PREPARE S1 FROM sqlQuery;
OPEN C1;
FETCH C1 INTO sqlResults;
--Now we can INsert into your target table
INSERT INTO <yourdatabase>.yourtargettable (customerID, customerName SQL_RESULT)
VALUES (customerID, customerName, sqlResults);
CLOSE C1;
END LOOP looplabel;
CLOSE demographic_cursor;
END

How to create database link dynamically

I am new in oracle db...
I need to create a database link by passing link name and connection string as variable.
DECLARE DBLINK_NAME varchar(100) :='newdblink';
Connection varchar(250) := '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=host.name.com)(Port=1521))(CONNECT_DATA=(SID=host)))';
BEGIN
EXECUTE IMMEDIATE 'CREATE DATABASE LINK' ||DBLINK_NAME||
'CONNECT TO SCHEMA_NAME IDENTIFIED BY password USING '||Connection;
END;
Can any one tell me what wrong in passing this variable value in execute statement?
I am doing using TOAD and oracle 11g.
You have missed the spaces around the database link name, so your command ends up as:
CREATE DATABASE LINKnewdblinkCONNECT TO ...
which will generate an ORA-01501: CREATE DATABASE failed error. You need to include a space after LINK and before CONNECT.
Your connect string also needs to be enclosed in single quotes, so you need to concatenate those as well, and they need to be escaped:
DECLARE
DBLINK_NAME varchar(100) :='newdblink';
Connection varchar(250) := '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=host.name.com)(Port=1521))(CONNECT_DATA=(SID=host)))';
BEGIN
EXECUTE IMMEDIATE 'CREATE DATABASE LINK ' || DBLINK_NAME
|| ' CONNECT TO SCHEMA_NAME IDENTIFIED BY password USING '''
|| Connection || '''';
END;
/
PL/SQL procedure successfully completed.
It's helpful to use dbms_output to display exactly what the execute immediate will try to run. You can often spot mistakes quickly - the missing spaces would have been pretty obvious - and can copy-and-patse the generated statement to run it as plain SQL, which can sometimes make other issues more obvious.
I'm not sure what the benefit of using PL/SQL, variables and dynamic SQL is here. You can just do a simple SQL statement using those values. Perhaps you intend to turn this into a procedure. But creating a link at run-time would be unusual.

Stored procedure - Multiple string parameters with IN clause sql

i want to filter the rows using Stored procedure. below is my query
Create PROCEDURE [dbo].[Test]
#SearchTerm VARCHAR(100)
AS
BEGIN
Select * from master where name in (#SearchTerm)
END
In code behind,
cmd.Parameters.AddWithValue("#SearchTerm", "peter")
when i run with above parameter, it's work fine.
but when i pass like this
cmd.Parameters.AddWithValue("#SearchTerm", "'peter','rahul'")
this time no rows fetching.
i tried manually then also it's not working.
exec Test ''peter','rahul''
Please help me, how to pass muliple string Using IN clause?
One method is
Create PROCEDURE [dbo].[Test]
#SearchTerm VARCHAR(100)
AS
BEGIN
Select * from master where ','+#SearchTerm+',' like '%,'+name+',%'
You can find more methods at http://www.sommarskog.se/arrays-in-sql-2008.html

Dynamically update ,delete ,insert GridView in ASP.NET

My database tables list in dropdownlist...when i select table from dropdown table display in GridView. I want to edit,delete & insert dynamically in GridView. Please give me solution....
Check out these links:
http://geekswithblogs.net/dotNETvinz/archive/2009/02/22/gridview-insert-edit-update-and-delete--the-ado.net-way.aspx
http://msdn.microsoft.com/en-us/library/aa479339.aspx
Say you have three database tables, Customer, Orders and Products - do you mean the names of these tables appear in your dropdownlist?
If so, when a table name is selected in the dropdownlist (and perhaps an 'Edit' button is clicked), you'll need to bind your GridView to the selected table's data.
You could do this with inline SQL - build it from the DDL:
string _selectString = "SELECT * FROM " + ddlTables.SelectedValue ; //Remember to include the schema in the dropdownlist's value property
And then use this SQL to fetch back the data and bind your grid to it.
A better way would be to wrap up the SQL in a stored procedure that uses SQL Server's INFORMATION_SCHEMA schema (which holds all the database's objects)
CREATE PROCEDURE MySchema.GetTableData
#TableName VARCHAR(Max),
#SchemaName VARCHAR(MAX) --Pass in the relevant Schema
AS
BEGIN
SET NOCOUNT ON
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = #TableName
AND TABLE_SCHEMA = #SchemaName
END
and get the data out this way. The only difference to the way you're probably already doing this is to set the SQLCommand's CommandType property to CommandType.StoredProcedure and to pass in the tablename and schema name as SQLParameters.
More information about ASP.Net and Stored Procedures:
http://www.c-sharpcorner.com/UploadFile/gtomar/storedprocedure12052007003126AM/storedprocedure.aspx
Once you've got the data from the table you just use the code & process linked to by #Brian.
hth.

Resources