It is giving me an error saying 'Incorrect syntax near the keyword 'convert'.' Could not able to figure out what could be done to remove it.
ALTER PROCEDURE [dbo].[sp_convert_unix_timestamp]
#table_name nvarchar(250), ---Staging Table
#DateTimeString nvarchar(max)
AS
BEGIN
if isnull(#DateTimeString,'') <> '' begin
declare #SQLString Nvarchar(max)
declare #UpdateString Nvarchar(max)
declare #initialepoch Nvarchar(10)
set #initialepoch = '19700101'
SELECT #SQLString = coalesce(#SQLString + ',', '') +
'convert(varchar, dateadd(ss, convert(Int, ' + value + '),' + #initialepoch + '), 120)'
FROM STRING_SPLIT(#DateTimeString, ',')
WHERE RTRIM(value) <> ''
select #UpdateString = 'Update [' + #table_name + '] set ' + #SQLString
exec (#SQLString)
end
END
Firstly, let's fix the SP so it's no open to Injection, and the datatype choice for #table_name:
ALTER PROCEDURE [dbo].[sp_convert_unix_timestamp]
#table_name sysname, ---Staging Table
#DateTimeString nvarchar(max)
AS
IF ISNULL(#DateTimeString, '') <> ''
BEGIN
DECLARE #SQLString nvarchar(MAX);
DECLARE #UpdateString nvarchar(MAX);
DECLARE #initialepoch nvarchar(10);
SET #initialepoch = N'19700101';
SELECT #SQLString = COALESCE(#SQLString + ',', '') + N'convert(varchar(10), dateadd(ss, convert(Int, ' + QUOTENAME(value,N'''') + N'), + #dinitialepoch), 120)'
FROM STRING_SPLIT(#DateTimeString, ',')
WHERE RTRIM(value) <> '';
SELECT #UpdateString = N'Update ' + QUOTENAME(#table_name) + N' set ' + #SQLString;
PRINT #UpdateString;
EXEC sp_executesql #SQLString, N'#dinitialepoch nvarchar(10)', #dinitialepoch = #initialepoch;
END;
If we were to run that SP, EXEC [dbo].[sp_convert_unix_timestamp] N'MyTable', N'20170720,20170721';, you'd get this output (and magcially, you know the reason why it's not working):
Update [MyTable] set convert(varchar, dateadd(ss, convert(Int, '20170720'), + #dinitialepoch), 120),convert(varchar, dateadd(ss, convert(Int, '20170721'), + #dinitialepoch), 120)
There is no column in your SET clause. You simply have UPDATE [Table] SET [Value]; The format of an UPDATE statement is UPDATE [Table] SET [Column] = [Value];. What column(s) that needs to be updated, however, I have no idea...
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here in sql server i want to get sql script by table name, In mysql server there is option for that SHOW CREATE TABLE client , can anyone please help how can we do this in sql server by script ?
CREATE PROCEDURE [dbo].[GENERATE_INSERTS] (#TABLENAME NVARCHAR(100),
#INCLUDE_COLUMN VARCHAR(MAX)=NULL,
#EXCLUDE_COLUMN VARCHAR(MAX) = NULL,
#TOP varchar(50)='',
#WHERECLAUSE NVARCHAR(MAX) = '',
#ORDERBYCLAUSE NVARCHAR(MAX) = '')
AS
BEGIN
SET NOCOUNT ON
--DECLARE VARIABLES
DECLARE #TABLEHASIDENTITY BIT
DECLARE #SQL NVARCHAR(MAX)
DECLARE #COLUMNLIST NVARCHAR(MAX)
DECLARE #VALUELIST NVARCHAR(MAX)
DECLARE #COLUMN_NAME VARCHAR(128)
DECLARE #DATA_TYPE VARCHAR(128)
--INITIALIZE VARIABLES
SET #COLUMNLIST = ''
SET #VALUELIST = ''
SET #COLUMN_NAME = ''
--CHECK IF THE TABLE HAS AN IDENTITY COLUMN
SELECT #TABLEHASIDENTITY = OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'TABLEHASIDENTITY')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = #TABLENAME
--CHECK IF WE NEED 'SET IDENTITY_INSERT TABLENAME ON' STATEMENT.
--IF THE TABLE HAS IDENTITY COLUMN, THE IDENTITY_INSERT WILL BE SET TO ON
IF #TABLEHASIDENTITY = 1
BEGIN
SET #SQL = 'SELECT TOP 1 ''SET IDENTITY_INSERT '
+ #TABLENAME + ' ON '' FROM ' + #TABLENAME
EXEC SP_EXECUTESQL
#SQL
END
--PREPARE LIST OF COLUMNS AND VALUES
--TO GET THE FIRST COLUMN'S ID
DECLARE NA_CUR CURSOR LOCAL FAST_FORWARD FOR
SELECT QUOTENAME(COLUMN_NAME),
DATA_TYPE from
INFORMATION_SCHEMA.COLUMNS C (NOLOCK)
WHERE TABLE_NAME = #TABLENAME
AND (EXISTS (SELECT TOKEN
FROM DBO.UDF_SPLITSTRING(#INCLUDE_COLUMN, ',') A WHERE C.TABLE_NAME=A.token )
--AND ( COLUMN_NAME IN (SELECT TOKEN
-- FROM DBO.UDF_SPLITSTRING(#INCLUDE_COLUMN, ','))
OR #INCLUDE_COLUMN IS NULL or #INCLUDE_COLUMN= '')
AND ( NOT EXISTS (SELECT 1
FROM DBO.UDF_SPLITSTRING(#EXCLUDE_COLUMN, ',') F
WHERE F.TOKEN = C.COLUMN_NAME)
OR #EXCLUDE_COLUMN IS NULL or #EXCLUDE_COLUMN = '' )
ORDER BY ORDINAL_POSITION ASC
OPEN NA_CUR
FETCH NA_CUR INTO #COLUMN_NAME, #DATA_TYPE
WHILE ##FETCH_STATUS = 0
BEGIN
--DETERMINING THE DATA TYPE OF THE COLUMN AND DEPENDING ON THE DATA TYPE,
--THE VALUES PART OF THE INSERT STATEMENT IS GENERATED.
SET #VALUELIST = #VALUELIST + CASE WHEN #DATA_TYPE IN ('CHAR', 'VARCHAR', 'NCHAR', 'NVARCHAR') THEN 'COALESCE('''''''' + REPLACE(RTRIM(' + #COLUMN_NAME + '),'''''''','''''''''''')+'''''''',''NULL'')'
WHEN #DATA_TYPE IN ('DATETIME', 'SMALLDATETIME') THEN 'COALESCE('''''''' + RTRIM(CONVERT(CHAR,' + #COLUMN_NAME + ',109))+'''''''',''NULL'')'
WHEN #DATA_TYPE IN ('UNIQUEIDENTIFIER') THEN 'COALESCE('''''''' + REPLACE(CONVERT(CHAR(255),RTRIM(' + #COLUMN_NAME + ')),'''''''','''''''''''')+'''''''',''NULL'')'
WHEN #DATA_TYPE IN ('TEXT', 'NTEXT') THEN 'COALESCE('''''''' + REPLACE(CONVERT(CHAR(8000),' + #COLUMN_NAME + '),'''''''','''''''''''')+'''''''',''NULL'')'
WHEN #DATA_TYPE IN ('BINARY', 'VARBINARY') THEN 'COALESCE(RTRIM(CONVERT(CHAR,' + 'CONVERT(INT,' + #COLUMN_NAME + '))),''NULL'')'
WHEN #DATA_TYPE IN ('TIMESTAMP', 'ROWVERSION') THEN 'COALESCE(RTRIM(CONVERT(CHAR,' + 'CONVERT(INT,' + #COLUMN_NAME + '))),''NULL'')'
WHEN #DATA_TYPE IN ('FLOAT', 'REAL', 'MONEY', 'SMALLMONEY') THEN 'COALESCE(LTRIM(RTRIM(' + 'CONVERT(CHAR, ' + #COLUMN_NAME + ',2)' + ')),''NULL'')'
ELSE 'COALESCE(LTRIM(RTRIM(' + 'CONVERT(CHAR, ' + #COLUMN_NAME + ')' + ')),''NULL'')' END + '+' + ''',''' + ' + '
--GENERATING THE COLUMN LIST FOR THE INSERT STATEMENT
SET #COLUMNLIST = #COLUMNLIST + #COLUMN_NAME + ','
PRINT #COLUMNLIST
PRINT #VALUELIST
FETCH NA_CUR INTO #COLUMN_NAME, #DATA_TYPE
END
CLOSE NA_CUR;
DEALLOCATE NA_CUR;
--END OF LOOP
--REMOVE EXTRA CHARACTERS THAT GOT CONCATENATED DURING THE LAST RUN THROUGH THE LOOP
SET #COLUMNLIST = LEFT(#COLUMNLIST, LEN(#COLUMNLIST) - 1)
SET #VALUELIST = LEFT(#VALUELIST, LEN(#VALUELIST) - 6)
print #COLUMNLIST
PRINT #VALUELIST
set #top = case when len(#top)>0 then 'top '+#TOP else '' end
--BUILD THE SQL STATEMENT
SET #SQL = 'SELECT '+#top+'''INSERT INTO [' + #TABLENAME + '] ('
+ #COLUMNLIST + ') ' + 'VALUES (''+' + #VALUELIST
+ '+'')'' FROM ' + #TABLENAME
--APPLY FILTER (WHERE CLAUSE) AND SORTING (ORDER BY CLAUSE) IF REQUIRED
IF LEN(#WHERECLAUSE) > 0
SET #SQL = #SQL + ' WHERE ' + #WHERECLAUSE
IF LEN(#ORDERBYCLAUSE) > 0
SET #SQL= #SQL + ' ORDER BY ' + #ORDERBYCLAUSE
--EXECUTE SQL STATEMENT TO PRINT INSERT STATEMENTS
PRINT #SQL
EXEC SP_EXECUTESQL
#SQL
--SET IDENTITY_INSERT OFF IF IT IS SET ON ABOVE
IF #TABLEHASIDENTITY = 1
BEGIN
SET #SQL = 'SELECT TOP 1 ''SET IDENTITY_INSERT '
+ #TABLENAME + ' OFF '' FROM ' + #TABLENAME
EXEC SP_EXECUTESQL
#SQL
END
END
I think the easiest way for you is using SQL Server Management studio and use the feature Generate Scripts.
More info: https://learn.microsoft.com/en-us/sql/relational-databases/scripting/generate-scripts-sql-server-management-studio
Hope it helps.
Edit:
After users feedback:
Lowell Izaguirre wrote a script that might help you. It is available on SQLServerCentral in this link http://www.sqlservercentral.com/scripts/SQL+Server+2005/67515/
Got from Here, hope it will hepl a bit.
CREATE proc GetTableScript (#table sysname)
as
declare #sql table(s varchar(1000), id int identity)
insert into #sql(s) values ('create table [' + #table + '] (')
insert into #sql(s)
select
' ['+column_name+'] ' +
data_type + coalesce('('+cast(character_maximum_length as varchar)+')','') + ' ' +
case when exists (
select id from syscolumns
where object_name(id)=#table
and name=column_name
and columnproperty(id,name,'IsIdentity') = 1
) then
'IDENTITY(' +
cast(ident_seed(#table) as varchar) + ',' +
cast(ident_incr(#table) as varchar) + ')'
else ''
end + ' ' +
( case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' +
coalesce('DEFAULT '+COLUMN_DEFAULT,'') + ','
from information_schema.columns where table_name = #table
order by ordinal_position
declare #pkname varchar(100)
select #pkname = constraint_name from information_schema.table_constraints
where table_name = #table and constraint_type='PRIMARY KEY'
if ( #pkname is not null ) begin
insert into #sql(s) values(' PRIMARY KEY (')
insert into #sql(s)
select ' ['+COLUMN_NAME+'],' from information_schema.key_column_usage
where constraint_name = #pkname
order by ordinal_position
update #sql set s=left(s,len(s)-1) where id=##identity
insert into #sql(s) values (' )')
end
else begin
update #sql set s=left(s,len(s)-1) where id=##identity
end
insert into #sql(s) values( ')' )
select s from #sql order by id
I am trying to insert data into a SQL Server table using a variable. I tried
DECLARE #table NVARCHAR(50) = 'ToolList',
#val NVARCHAR(50) = 'test'
EXEC ('INSERT INTO ' + #table + 'SELECT ' + #val)
and
EXEC ('INSERT INTO ' + #table + '([col1]) VALUES(' + #val +')'
but still get an error that says
Incorrect syntax near 'test'.
you missed a space before SELECT and the #val should enclosed in single quote
DECLARE #table nvarchar(50) = 'ToolList',
#val nvarchar(50) = 'test'
EXEC ( 'INSERT INTO ' + #table + ' SELECT ''' + #val + '''')
when you use Dynamic SQL, it is easier to form the query in a variable so that you can print out , inspect the value before execution
select #sql = 'INSERT INTO ' + #table + ' SELECT ''' + #val + ''''
print #sql
exec (#sql)
You'd better use sp_executesql that allows for statements to be parameterized, to avoid the risk of SQL injection.
DECLARE #Query NVARCHAR(1000),
#table NVARCHAR(50) = 'ToolList'
SET #Query = 'INSERT INTO ' + #table + ' SELECT #val'
EXEC sp_executesql #Query, N'#val nvarchar(50)', #val = 'test'
sp-executesql-transact-sql
You can also use CHAR(39) instead of adding single quotes every time for better readability. And also, you have not added a space after the variable which contains the table name.
Query
declare #table nvarchar(50) = 'ToolList',
#val nvarchar(50) = 'test2';
declare #sql as varchar(max) = 'insert into ' + #table
+ ' select ' + char(39) + #val + char(39);
exec(#sql);
You need 4 singlequotes before the #val field as it is a string and all strings needs to be encapsulated in single quotes.
You can print the dynamic string using PRINT command check what the final string you are going to execute.
DECLARE #table VARCHAR(50) = 'ToolList'
DECLARE #val VARCHAR(50) = 'test'
DECLARE #DSQL AS VARCHAR(MAX) = ''
SET #DSQL = #DSQL + ' INSERT INTO [' + #table + ']' + '
SELECT ' + '''' + #val + ''''
--PRINT #DSQL
EXEC(#DSQL)
I am writing an audit trigger which saves every change occurred in the table in another table. But I receive this error when I am trying to alter the Users table:
Operand type clash: int is incompatible with unique identifier
The Users table contains a uniqueidentifier column, among others.
Here is the part of the code where the error occurs:
ALTER TRIGGER [dbo].[UsersLogger]
ON [dbo].[Users]
FOR INSERT, DELETE, UPDATE
AS
BEGIN
DECLARE #audit_oldvalue sql_variant;
DECLARE #audit_value sql_variant;
DECLARE #audit_field varchar(100);
DECLARE #sql nvarchar(max);
DECLARE #ParmDefinition nvarchar(max) ;
DECLARE #OutString varchar(max) ;
DECLARE #converted_uid nvarchar(50);
DECLARE #qid int ;
DECLARE #Cinfo VARBINARY(128)
SELECT #Cinfo = Context_Info()
IF #Cinfo = 0x55555
RETURN
DECLARE #Action as char(1);
SET #Action = (CASE WHEN EXISTS(SELECT * FROM INSERTED)
AND EXISTS(SELECT * FROM DELETED)
THEN 'U' -- Set Action to Updated.
ELSE NULL -- Skip. It may have been a "failed delete".
END)
SET #OutString = ''
SELECT *
INTO #tempTrigT
FROM
(SELECT *
FROM deleted
WHERE #Action IN ( 'U', 'D')) A
UNION
(SELECT *
FROM inserted
WHERE #Action = 'I')
SET #sql = ''
if #Action = 'U'
BEGIN
Select #sql = #sql + 'Case when IsNull(i.[' + Column_Name +
'],0) = IsNull(d.[' + Column_name + '],0) then ''''
else ' + quotename(Column_Name, char(39)) + ' + '',''' + ' end +'
from information_schema.columns
where table_name = 'Users' and column_name <>'rowguid' and column_name <>'modifieddate'
set #ParmDefinition = '#OutString varchar(max) OUTPUT'
set #sql = 'Select #OutString = '
+ Substring(#sql,1 , len(#sql) -1) +
' From dbo.Users i '
+ ' inner join #tempTrigT d on
i.id = d.id'
exec sp_executesql #sql, #ParmDefinition, #OutString OUT
END
DECLARE #Items VARCHAR(max)
set #Items = #OutString;
DECLARE #Item VARCHAR(50)
DECLARE #Pos INT
DECLARE #Loop BIT
SELECT #Loop = CASE WHEN LEN(#OutString) > 0 THEN 1 ELSE 0 END
WHILE (SELECT #Loop) = 1
BEGIN
SELECT #Pos = CHARINDEX(',', #OutString, 1)
IF #Pos > 0
BEGIN
SELECT #Item = SUBSTRING(#OutString, 1, #Pos - 1)
SELECT #OutString = SUBSTRING(#OutString, #Pos + 1, LEN(#OutString) - #Pos)
IF (TRY_CONVERT(UNIQUEIDENTIFIER, #Item) is not null)
begin
select #Item = convert(nvarchar(50), #Item)
end
If I had to guess, I'd blame it on
Select #sql = #sql + 'Case when IsNull(i.[' + Column_Name +
'],0) = IsNull(d.[' + Column_name + '],0) then ''''
else ' + quotename(Column_Name, char(39)) + ' + '',''' + ' end +'
from information_schema.columns
where table_name = 'Users' and column_name <>'rowguid' and column_name <>'modifieddate'
It's because you're doing an ISNULL on a UNIQUEIDENTIIFIER column and setting the value to an INT. You could change from ISNULL(i.[ColumnName], 0) to ISNULL(i.[ColumnName], '') however, that is assuming all the columns in your tables are string applicable datatypes. Otherwise, you could do some conditional logic on the replacement value for the ISNULL function by using the [DATA_TYPE] column in that table.
I am trying to write a stored procedure that will removed leading and trailing quotes from an arbitrary table and column. I keep getting an error saying that the table name isn't declared.
Here is the SP
create table [dbo].[test] (id nvarchar(20))
insert into dbo.test values ('"07617966004766"')
go
CREATE PROCEDURE sp_stripDoubleQuotes
#tableName sysname,
#columnName sysname
AS
BEGIN
SET NOCOUNT ON;
UPDATE #tableName
SET #columnName = SUBSTRING(#columnName, 2, LEN(#columnName))
WHERE LEFT(#columnName, 1) = '"'
UPDATE #tableName
SET #columnName = SUBSTRING(#columnName, 1, LEN(#columnName)-1)
WHERE RIGHT(#columnName, 1) = '"'
END
GO
exec [dbo].[sp_stripDoubleQuotes] N'[dbo].[test]', N'[id]'
select * from test
Here is a link to a fiddle:
link to fiddle
CREATE PROCEDURE sp_stripDoubleQuotes
#tableName sysname,
#columnName sysname,
#SQL varchar(MAX)
AS
BEGIN
SET NOCOUNT ON;
SET #SQL =
'UPDATE ' + '[' + #tableName +']' +
'SET' + '[' + #columnName +']' +'= SUBSTRING(' +'[' + #columnName +']' +', 2, LEN(' +'[' + #columnName +']' +'))
WHERE LEFT(' + '[' + #columnName +']' +', 1) = '+'''"'''
--PRINT(#SQL)
EXEC (#SQL)
SET #SQL =
'UPDATE ' + '[' + #tableName +']' +
'SET' + '[' + #columnName +']' +'= SUBSTRING(' + '[' + #columnName +']' +', 1, LEN(' + '[' + #columnName + ']' +')-1)
WHERE RIGHT(' + '[' + #columnName +']' +', 1) = '+'''"'''
--PRINT(#SQL)
EXEC (#SQL)
END
GO
exec [dbo].[sp_stripDoubleQuotes] N'test', N'id' -- exec [dbo].[sp_stripDoubleQuotes] N'[dbo].[test]', N'[id]'
Updated 2nd: I added [] to wrap table and column incase your table and column name have whitespace in them. Thanks #Sean Lange and #Richard
Updated 3rd: As #[benjamin moskovits] (xD) mentioned, if you hard coded brackets, the correct execute command is exec [dbo].[sp_stripDoubleQuotes] N'test', N'id'. Try to add or remove brackets and print to see whether the syntax is correct before executing it.
CREATE PROCEDURE sp_stripDoubleQuotes
#tableName sysname,
#columnName sysname
AS
BEGIN
SET NOCOUNT ON;
declare #QuerytoExecute varchar(1000)
set #QuerytoExecute="UPDATE "+#tableName+"
SET "+#columnName+" = SUBSTRING("+#columnName+", 2, LEN("+#columnName+"))
WHERE LEFT("+#columnName+", 1) = '""'";
exec (#QuerytoExecute);
set #QuerytoExecute="
UPDATE "+#tableName+"
SET "+#columnName+" = SUBSTRING("+#columnName+", 1, LEN("+#columnName+")-1)
WHERE RIGHT("+#columnName+", 1) = '""'";
exec (#QuerytoExecute);
END
GO
If i have a comma separated list of values:
A1,B2,B3
How do i pass this into a variable and then form it into an SQL IN statement.
DECLARE #DATE AS VARCHAR(50)
SELECT #DATE = CONVERT(VARCHAR(8), Getdate(), 112)
--PRINT #DATE
DECLARE #TIME AS VARCHAR(50)
--PRINT TIME
SELECT #TIME = Replace(CONVERT(VARCHAR(5), Getdate(), 108), ':', '')
DECLARE #ID AS VARCHAR(50)
SELECT #ID = Replace(W0128001, 32322, 32323, 3232323, 2323232, ',', ',')
--PRINT #ID
DECLARE #QUERY NVARCHAR(MAX);
SET #QUERY = 'SELECT * INTO BACKUPTABLE_' + #DATE + #TIME
+ '
FROM TABLE
WHERE ID IN (' + '''' + #ID + ''')'
--EXEC #query
PRINT #QUERY
I have tried to do a replace above but i want it so that an end user can PASTE into the values and my script will take care of the commas and properly form it. It should also strip out the last commas from the end.
My output needs to read:
SELECT * INTO BACKUPTABLE_201606061503
FROM TABLE
WHERE ID IN ('W0128001','32322','32323','3232323','2323232')
For one thing, you don't surround it with single quotes:
SET #QUERY = 'SELECT * INTO BACKUPTABLE_' + #DATE + #TIME + '
FROM TABLE
WHERE ID IN (' + #ID + ')';
There are other ways to pass comma-delimited values to a SQL statement, including using a split() function or XML.
CREATE PROCEDURE [dbo].[CreateBackupTable]
#ID varchar(100) = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE #DATE VARCHAR(50)= CONVERT(VARCHAR(8), Getdate(), 112);
DECLARE #TIME VARCHAR(50) = Replace(CONVERT(VARCHAR(5), Getdate(), 108), ':', '')
declare #xml xml,#SQL NVARCHAR(MAX);
set #xml = N'<root><r>' + replace(#ID,',','</r><r>') + '</r></root>'
SET #SQL = N' SELECT * INTO ' + QUOTENAME('BACKUPTABLE_' + #DATE + #TIME)
+ N' from TableName '
+ N' where ID IN (
select r.value(''.'',''varchar(max)'') as item
from #xml.nodes(''//root/r'') as records(r)
)'
exec sp_executesql #sql
, N'#ID varchar(100), #xml XML'
, #ID
, #Xml
END