I want to create a database table with current date as tablename using sql query
In SQL Server you can name a table basically anything you want by enclosing it in [].
So generate dynamic sql and execute it:
EXEC sp_executesql N'create table [12/10/2014] (ab varchar(10));'
Further you can get the whole thing dynamically:
DECLARE #sql NVARCHAR(MAX) = 'create table [' + CONVERT(nvarchar(30), GETDATE(), 101) + '] (ab varchar(10))'
EXEC sp_executesql #sql
EDIT: This was originally tagged as MySQL so here is the solution for that as well, because I was curious.
SELECT DATE_FORMAT(CURDATE(), '%m/%d/%Y');
SET #SQL =CONCAT('CREATE TABLE `' ,DATE_FORMAT(CURDATE(), '%m/%d/%Y'), '` (id int(10))');
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Though I think it's not a good option, You can get the current date from C# using
DateTime.Today
It can look like
using (SqlConnection con = new SqlConnection(conStr))
{
try
{
con.Open();
using (SqlCommand command = new SqlCommand("CREATE TABLE " + "someNamingPrefixHere_" + DateTime.Today + " (ab varchar(10));", con))
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
first:
do you really need to create a table that has a name as current date? why not create only a table and add a field that is a date and keep adding your data filling the date field with the current date?
if you really need to create many tables then:
you can't have a table name with the "/" character, you will have to replace those with something that is accepted by MySQL or SQLServer.
anyway to get the current date in c# use the DateTime.Now Property in c# and then you can format it using the DateTime.ToString Method (String) passing the format you wish to use like:
string tableName = DateTime.Now.ToString("ddMMyyyy");
this will give you a result like "29102014" if it's 29th october 2014.
then you can create the sql statement using the sql syntax from MySQL or SQLServer, open a connection, execute the command and then close the connection.
You should seriously learn about some SQL design practices. One file should be a row, but one day shouldn't be a whole table. I can't provide any specific answer with this description of your "problem", but easiest way would be just add another column "date" and that's it. Finished work. It's insane to have a table for each day.
This is how I obtained a table name with date and time like MY_TABLE_2019-03-25_08h45m32s687 in MS SQL server:
DECLARE #MIGRATION_TABLE_1 VARCHAR(100);
SET #MIGRATION_TABLE_1= convert(varchar(25), getdate(),121);
SET #MIGRATION_TABLE_1=REPLACE(#MIGRATION_TABLE_1,' ','_');
SET #MIGRATION_TABLE_1=STUFF(#MIGRATION_TABLE_1,14,1,'h')
SET #MIGRATION_TABLE_1=STUFF(#MIGRATION_TABLE_1,17,1,'m')
SET #MIGRATION_TABLE_1=STUFF(#MIGRATION_TABLE_1,20,1,'s')
SET #MIGRATION_TABLE_1='MY_TABLE_'+#MIGRATION_TABLE_1;
The STUFF function calls are for replacing the , : and . in the default date format
2019-03-25 08:45:32.687 to
2019-03-25_08h45m32s687
At this stage, you can display the content of the variable #MIGRATION_TABLE_1 with
SELECT #MIGRATION_TABLE_1 AS VARCHAR;
Then, you have to use the variable name into an EXEC statement as everything has to be dynamic, for example :
EXEC('CREATE TABLE ' + #tablename + ' REST OF YOUR ARGUMENTS);
Related
I'm using Microsoft SQL server management studio.
I would like to add a new column to a table (altertable1), and name that column using the data from a cell (Date) of another table (stattable1).
DECLARE #Data nvarchar(20)
SELECT #Data = Date
FROM stattable1
WHERE Adat=1
DECLARE #sql nvarchar(1000)
SET #sql = 'ALTER TABLE altertable1 ADD ' + #Data + ' nvarchar(20)'
EXEC (#sql)
Executing this, I get the following error and can't find out why:
"Incorrect syntax near '2021'."
The stattable1 looks like this:
Date |Adat
2021-09-08 |1
2021-09-08 is a daily generated data:
**CONVERT(date,GETDATE())**
Just like Larnu said in comment, maybe this is not a main problem for you, but if you want to do this add [ ] when you want to name column starting with number.
Like this:
SET #sql = 'ALTER TABLE altertable1 ADD [' + #Data + '] nvarchar(20)'
And of course, naming columns by date or year is not best practice.
The problem with your overall design is that you seem to be adding a column to the table every day. A table is not a spreadsheet and you should be storing data for each day in a row, not in a separate column. If your reports need to look that way, there are many ways to pivot the data so that you can handle that at presentation time without creating impossible-to-maintain technical debt in your database.
The problem with your current code is that 2021-06-08 is not a valid column name, both because it starts with a number, and because it contains dashes. Even if you use a more language-friendly form like YYYYMMDD (see this article to see what I mean), it still starts with a number.
The best solution to the local problem is to not name columns that way. If you must, the proper way to escape it is to use QUOTENAME() (and not just manually slap [ and ] on either side):
DECLARE #Data nvarchar(20), #sql nvarchar(max);
SELECT #Data = Date
FROM dbo.stattable1
WHERE Adat = 1;
SET #sql = N'ALTER TABLE altertable1
ADD ' + QUOTENAME(#Data) + N' nvarchar(20);';
PRINT #sql;
--EXEC sys.sp_executesql #sql;
This also demonstrates your ability to debug a statement instead of trying to decipher the error message that came from a string you can't inspect.
Some other points to consider:
if you're declaring a string as nvarchar, and especially when dealing with SQL Server metadata, always use the N prefix on any literals you define.
always reference user tables with two-part names.
always end statements with statement terminators.
generally prefer sys.sp_executesql over EXEC().
some advice on dynamic SQL:
Protecting Yourself from SQL Injection - Part 1
Protecting Yourself from SQL Injection - Part 2
I have been researching this for a couple of days and feel like I am going around in circles. I have basic knowledge of SQL but there are many areas I do not understand.
I have a table that stores the names and fields of all the other tables in my database.
tblFields
===================================================
TableName FieldName BookmarkName
---------------------------------------------------
Customer FirstName CustomerFirstName
Customer LastName CustomerLastName
Customer DOB CustomerDOB
I want to write a SELECT statement like the following but i am unable to get it work:
SELECT (SELECT [FieldName] FROM [TableName]) FROM tblFields
Is this possible? The application I have developed requires this for user customization of reports.
If i understand what you are trying to do, i think this will help you. It is not pretty and it works for SQL Server 2005 and above, but maybe this is what you are looking for:
declare #tableName nvarchar(100)
declare #sqlQuery nvarchar(max)
declare #fields varchar(500)
set #tableName = 'YourTableName'
set #fields = ''
select #fields = #fields + QUOTENAME(t.fieldname) + ',' from (
select distinct fieldname from tblfields where tablename = #tableName)t
set #sqlQuery = 'select ' + left(#fields, LEN(#fields)-1) + ' from ' + QUOTENAME(#tableName)
execute sp_executesql #sqlQuery
Edit: As Martin suggested, i edited so that the columns and tablename are using QUOTENAME
If I understand correctly what you are trying to do, you are probably better off doing this as two separate queries from your program. One which gets the fields you want to select which you then use in your program to build up the second query which actually gets the data.
If it must be done entirely in SQL, then you will need to tell us what database you are using. If it is SQL Server, you might be able to user a cursor over the first query to build up the second query which you then execute with the sp_executesql stored procedure. But doing doing it outside of SQL would be recommended.
I'm using a dynamic query in stored procedure as a tableadapter while creating a dataset in visual studio 2013. The problem is Data Column is not showing up Parameters and field for SELECT operation. Please find the screenshot:
I'm not sure if this dynamic statement will work in this case.. Anybody know any possible way to use this stored procedure as datasource in my report.
query in stored procedure.
DECLARE #DynamicSQL nvarchar(4000)
SET #DynamicSQL = N'SELECT * FROM ' +#TableName +
' WHERE ClassName='+#OperationName+' AND (RunTime BETWEEN '+#Date1+' AND '+#Date2+')
AND TestCaseId = ISNULL('+#TestCaseId+', TestCaseId)
AND TestRunResult = ISNULL('+#TestRunResult+',TestRunResult)
EXECUTE sp_executesql #DynamicSQL
If the schema doesn't change then you can try this:
Start your stored procedure with something like this pseudo-code so SSRS knows what columns to expect:
IF 1=0
SELECT
CAST('SomeDummyData' AS Field1DataType) AS Field1Name
, CAST('SomeDummyData' AS Field2DataType) AS Field2Name
, ETC...
ELSE
...the rest of your stored procedure
If that's not enough, open your DataSet and hard-code the fields in the Fields tab.
I have a NVARCHAR(max) column in a table and a stored procedure that would update this column as well as any other column in the table using CASE switching:
CREATE PROCEDURE updateTable
#columnName sysname,
#value nvarchar(max)
AS
UPDATE [dbo].[TestTable]
SET
BigNvarcharValue = CASE #columnName WHEN 'BigNvarcharValue' THEN #value ELSE BigNvarcharValue END,
TableName = CASE #columnName WHEN 'TableName' THEN #value ELSE TableName END
All is good if I execute this procedure from SQL Management Studio with
EXEC [dbo].[updateTable]
#columnName = 'BigNvarcharValue',
#value = N'SOME BIG 80Kb value'
I can also update TableName from C# code using the same stored procedure, but when it comes to updating this BigNvarcharValue from C#, it fails with SQLException that "String or binary data would be truncated". Now, I figured it has something to do with CASE in this stored procedure, because when I break it to a simpler stored procedure, everything works fine:
CREATE PROCEDURE updateTable
#columnName sysname,
#value nvarchar(max)
AS
UPDATE [dbo].[TestTable]
SET BigNvarcharValue=#value
I read a bunch of forum posts that describe this problem of trying to insert a bigger value into NVARCHAR column that would cause this exception, but it doesnt seem to apply.
I'm fairly new to T-SQL, so are there any limitations of CASE that I dont know of?
P.S. BigNvarcharValue is NVARCHAR(MAX) and TableName is NVARCHAR(50)
What are the data types of the columns you're dealing with? Because I've reproduced the error by attempting to insert a value that is allowed by NVARCHAR(max) into a column that is VARCHAR(50).
To reiterate - NVARCHAR(max) is allowing you to specify a value that is longer than the stated data type, which is why you get the error about truncation.
The error says it by itself, "String or binary data would be truncated". This means that you seem to insert a larger value than what the nvarchar(max) can handle.
SSMS 2008 has some debugging features allowing to set breakpoints, etc.
I think you might wish to take an eye out to the System.String maximum capacity either. This is only a matter of length, somewhere.
With your exact same stored procedure and the table you described I ran the following code
class Program
{
static void Main(string[] args)
{
using(SqlConnection cnn = new SqlConnection(#"Server=.;Database=test;Trusted_Connection=True;"))
{
cnn.Open();
SqlCommand cmd = new SqlCommand("updateTable",cnn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("#columnName",
System.Data.SqlDbType.NVarChar, 128));
cmd.Parameters["#columnName"].Value = "BigNvarcharValue";
cmd.Parameters.Add(new SqlParameter("#value",
System.Data.SqlDbType.NVarChar, -1));
cmd.Parameters["#value"].Value = new string('T', 80000);
cmd.ExecuteNonQuery();
}
}
}
It worked fine. I would inspect the command text and the parameter collection (name and value) and verify every is as you think it is.
Thanks everyone for the responses. I ended up separating update of the big column to an individual procedure, which solved the problem. Im sure the culprit was with the CASE statement.
I have about 100 sites built in a cms, each with its own database. Each database has the same tables.
My stored procedure needs to select all of the pages in a database given a sitename.
Below I am trying to pass the database name as a parameter, but it doesn't seem to work.
...
#site nvarchar(250)
AS
SELECT *
FROM #site..cmsDocument
WHERE published = 1
Is there some other way to do this?
SELECT #dbname = quotename(dbname)
SELECT #sql = ' SELECT ... FROM ' + #dbname + '.dbo.tablename WHERE ...'
EXEC sp_executesql #sql, #params, ...
Refs:
sp_executesql (Transact-SQL)
The Curse and Blessings of Dynamic SQL
You can specify the database in the connection string:
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Almost no DBMSes allow you to use a parameter in this manner. Build a dynamic query via concatenation and execute it.
You can use the undocumented stored procedure sp_MSforeachdb:
sp_MSforeachdb 'SELECT * FROM ?..table_name_here'
If the query / procedure is being executed from a .NET application, you can also programmatically change the database of the connection object (VB.NET):
Using da As New SqlDataAdapter("SELECT * FROM cmsDocument WHERE published = 1", GetDatabaseConnection(ServerName))
da.SelectCommand.Connection.ChangeDatabase("Foobar")
End Using