Replacing more than 1 period to 1 period in sql - sql

I have the following code to convert more than one period to one period in a column of a table.
alter proc replace_characters_1
#COLUMN_NAME varchar(30),
#TABLE_NAME varchar(30)
as
declare #SQL varchar(MAX)
while #COLUMN_NAME like '%..%'
begin
set #SQL= 'update [' +#TABLE_NAME+ '] set [' +#COLUMN_NAME+ '] = replace([' +#COLUMN_NAME+ '],''..'',''.'')';
exec(#SQL)
end
I want to change the Anna...Amal to Anna.Amal with one go, but the loop is not working. What should I do?`

One possible approach is to use nested REPLACE()s:
SET ColumnName = REPLACE(REPLACE(REPLACE(ColumnName, '.', '<>'), '><', ''), '<>', '.')
After the first REPLACE() the part from the text that contains periods (.) looks like <><><>. After the second REPLACE() the result is only <> and the final REPLACE() returns single period (.). If the characters < and > exist in the input text, you can choose another pair of characters.
Table:
CREATE TABLE Data (Name varchar(100))
INSERT INTO Data (Name)
VALUES
('ANNA..Amal'),
('ANNA..Amal.'),
('ANNA.Amal.'),
('ANNA...........Amal.'),
('ANNA.....Amal')
Procedure:
CREATE PROC replace_characters_1
#COLUMN_NAME sysname,
#TABLE_NAME sysname
AS
BEGIN
DECLARE #SQL nvarchar(MAX)
DECLARE #RC int
SET #SQL =
N'UPDATE ' + QUOTENAME(#TABLE_NAME) + N' ' +
N'SET ' + QUOTENAME(#COLUMN_NAME) + N' = ' +
N'REPLACE(REPLACE(REPLACE(' + QUOTENAME(#COLUMN_NAME) + ', ''.'', ''<>''), ''><'', ''''), ''<>'', ''.'') ' +
N'WHERE ' + QUOTENAME(#COLUMN_NAME) + N' LIKE ''%..%'''
EXEC #RC = sp_executesql #SQL
RETURN #RC
END
Result:
EXEC replace_characters_1 N'Name', N'Data'
SELECT * FROM Data
Name
ANNA.Amal
ANNA.Amal.
ANNA.Amal.
ANNA.Amal.
ANNA.Amal

Here is an approach that will reduce repeating characters.
Example
Declare #YourTable Table ([SomeCol] varchar(50))
Insert Into #YourTable Values
('Anna...Amal')
,('Anna........Amal')
,('Anna.Amal')
,('Anna Amal')
Select *
,NewVal = replace(replace(replace(SomeCol,'.','†‡'),'‡†',''),'†‡','.')
from #YourTable
Returns
SomeCol NewVal
Anna...Amal Anna.Amal
Anna........Amal Anna.Amal
Anna.Amal Anna.Amal
Anna Amal Anna Amal

Please check Zhorov's answer as it avoids multiple operations like this one.
CREATE PROCEDURE replace_characters_1
#COLUMN_NAME varchar(30),
#TABLE_NAME varchar(30)
AS
BEGIN
DECLARE #SQL NVARCHAR(MAX) = N'
UPDATE T SET
' + QUOTENAME(#COLUMN_NAME) + N' = REPLACE(' + QUOTENAME(#COLUMN_NAME) + N',''..'',''.'')
FROM
' + QUOTENAME(#TABLE_NAME) + N' AS T
WHERE
T.' + QUOTENAME(#COLUMN_NAME) + N' LIKE ''%..%'';
SET #UpdatedRowsOut = ##ROWCOUNT;';
DECLARE #UpdatedRows INT = 1;
WHILE #UpdatedRows > 0
BEGIN
EXECUTE sp_executesql
#SQL,
N'#UpdatedRowsOut INT OUTPUT',
#UpdatedRowsOut = #UpdatedRows OUTPUT;
END
END
Dynamic SQL is now returning the amount of rows that were updated, so it keeps going as long as there are values with .. for that column (note that there is a WHERE filter, you don't want to update all rows every time!).
SQL Server isn't the best for regex expressions, maybe you want to consider using a CLR function if you need to do different kind of stuff with regex.

I am using CHARINDEX and STUFF to derive the resultset
DECLARE #sqlstring Varchar(max) = 'Anna...Amal'
-- Getting first index of .
DECLARE #firstidx INT = CHARINDEX('.',#sqlstring)
-- Getting last index of .
Declare #lastidx int = (LEN(#sqlstring) - CHARINDEX('.',REVERSE(#sqlstring))) + 1
-- Stuffing the gap with emptystring
SELECT STUFF(#sqlstring,#firstidx+1,(#lastidx-#firstidx),'') as result
Result
+-----------+
| result |
+-----------+
| Anna.Amal |
+-----------+
UPDATE: If there are multiple comma separated values
DECLARE #sqlstring Varchar(max) = 'Anna...Amal,Vimal...Mathew'
SELECT STRING_agg(removedvalues,',') as values
FROM
(SELECT STUFF(value, CHARINDEX('.',value)+1
,(LEN(value) - CHARINDEX('.',REVERSE(value)) + 1) - CHARINDEX('.',value),'') AS removedvalues
FROM string_split(#sqlstring,',') ) AS t
+------------------------+
| Values |
+------------------------+
| Anna.Amal,Vimal.Mathew |
+------------------------+

Related

Html output of a stored procedure into a table or variable

I have a stored procedure dbo.someprocedure which takes input as sql query and returns Html table format.
Exec someprocedure 'Select * from dbo.Table'
This html I would like to insert into a table or a variable so that I can perform few changes on the result html.
I tried using
Declare #variable varchar(max)
Exec #variable = someprocedure 'Select * from dbo.Table'
it is executing but returning 0 the variable is not assigned.
Thanks in advance, looking for your answer.
I found an alternative procedure, which served my purpose.
CREATE PROC [dbo].[spQueryToHtmlTable]
(
#query nvarchar(MAX),
#orderBy nvarchar(MAX) = NULL,
#html nvarchar(MAX) = NULL OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
IF #orderBy IS NULL BEGIN
SET #orderBy = ''
END
SET #orderBy = REPLACE(#orderBy, '''', '''''');
DECLARE #realQuery nvarchar(MAX) = '
DECLARE #headerRow nvarchar(MAX);
DECLARE #cols nvarchar(MAX);
SELECT * INTO #dynSql FROM (' + #query + ') sub;
SELECT #cols = COALESCE(#cols + '', '''''''', '', '''') + ''['' + name + ''] AS ''''td''''''
FROM tempdb.sys.columns
WHERE object_id = object_id(''tempdb..#dynSql'')
ORDER BY column_id;
SET #cols = ''SET #html = CAST(( SELECT '' + #cols + '' FROM #dynSql ' + #orderBy + ' FOR XML PATH(''''tr''''), ELEMENTS XSINIL) AS nvarchar(max))''
EXEC sys.sp_executesql #cols, N''#html nvarchar(MAX) OUTPUT'', #html=#html OUTPUT
SELECT #headerRow = COALESCE(#headerRow + '''', '''') + ''<th>'' + name + ''</th>''
FROM tempdb.sys.columns
WHERE object_id = object_id(''tempdb..#dynSql'')
ORDER BY column_id;
SET #headerRow = ''<tr>'' + #headerRow + ''</tr>'';
SET #html = ''<table border="1">'' + #headerRow + #html + ''</table>'';
';
EXEC sys.sp_executesql #realQuery, N'#html nvarchar(MAX) OUTPUT', #html=#html OUTPUT
END
GO
DECLARE #html nvarchar(MAX);
EXEC spQueryToHtmlTable #html = #html OUTPUT, #query = N'SELECT * FROM sometable';
#html contains the html output so that u can perform your operations on the html reformatting
example:
select #htmlTable = Concat('<html>
<style>
table {
border-collapse: collapse;
width: 50%;
}
td, th {
border: 1px solid #B8B8B8;
text-align: left;
padding: 8px;
}
thead {
color:white;
}
</style>
<body>
<table>
<tr>
<th bgcolor="#B8B8B8">col1</th>
<th bgcolor="#B8B8B8">col2</th>
<th bgcolor="#B8B8B8">col3</th>
</tr>',substring(#html,charindex('</tr>',#html,1)+5,len(#html)),'</body></html>')
thanks for your answers.
What #SeanLange said is right. If you want to use SQL stored procedure to convert SQL table into HTML, you must use output when you execute SQL stored procedure.
For example - my table:
Create stored procedure
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[CustomTable2HTMLv4]
(#TSQL_QUERY NVARCHAR(4000),
#OUTPUT NVARCHAR(MAX) OUTPUT,
#TBL_STYLE NVARCHAR(1024) = '',
#ALIGNMENT INT = 0)
AS
-- #exec_str stores the dynamic SQL Query
DECLARE #exec_str NVARCHAR(MAX)
-- #ParmDefinition stores the parameter definition for the dynamic SQL
DECLARE #ParmDefinition NVARCHAR(500)
IF #ALIGNMENT = 0
BEGIN
-- We need to use dynamic SQL at this point so we can expand the input table name parameter
SET #exec_str = N'DECLARE #exec_str NVARCHAR(MAX)
DECLARE #ParmDefinition NVARCHAR(500)
DECLARE #DEBUG INT
SET #DEBUG = 0
IF #DEBUG=1 Print ''Table2HTML -Horizontal alignment''
-- Make a copy of the original table adding an indexing column. We need to add an index column to the table to facilitate sorting so we can maintain the
-- original table order as we iterate through adding HTML tags to the table columns.
-- New column called CustColHTML_ID (unlikely to be used by someone else!)
--
select CustColHTML_ID=0,* INTO #CustomTable2HTML FROM (' + #TSQL_QUERY + ') SUB
IF #DEBUG=1 PRINT ''Created temporary custom table''
--Now alter the table to add the auto-incrementing index. This will facilitate row finding
DECLARE #COUNTER INT
SET #COUNTER=0
UPDATE #CustomTable2HTML SET #COUNTER = CustColHTML_ID=#COUNTER+1
IF #DEBUG=1 PRINT ''Added counter column to custom table''
-- #HTMLROWS will store all the rows in HTML format
-- #ROW will store each HTML row as fields on each row are iterated through
-- using dynamic SQL and a cursor
-- #FIELDS will store the header row for the HTML Table
DECLARE #HTMLROWS NVARCHAR(MAX) DECLARE #FIELDS NVARCHAR(MAX)
SET #HTMLROWS='''' DECLARE #ROW NVARCHAR(MAX)
-- Create the first HTML row for the table (the table header). Ignore our indexing column!
SELECT #FIELDS=COALESCE(#FIELDS, '' '','''')+''<td>'' + name + ''</td>''
FROM tempdb.sys.Columns
WHERE object_id=object_id(''tempdb..#CustomTable2HTML'')
AND name not like ''CustColHTML_ID''
SET #FIELDS=#FIELDS + ''</tr>''
IF #DEBUG=1 PRINT ''table fields: '' + #FIELDS
-- #ColumnName stores the column name as found by the table cursor
-- #maxrows is a count of the rows in the table, and #rownum is for marking the
-- ''current'' row whilst processing
DECLARE #ColumnName NVARCHAR(500)
DECLARE #maxrows INT
DECLARE #rownum INT
--Find row count of our temporary table
SELECT #maxrows=count(*) FROM #CustomTable2HTML
--Create a cursor which will look through all the column names specified in the temporary table
--but exclude the index column we added (CustColHTML_ID)
DECLARE col CURSOR FOR
SELECT name FROM tempdb.sys.Columns
WHERE object_id=object_id(''tempdb..#CustomTable2HTML'')
AND name not like ''CustColHTML_ID''
ORDER BY column_id ASC
--For each row, generate dynamic SQL which requests the each column name in turn by
--iterating through a cursor
SET #rowNum=1
SET #ParmDefinition=N''#ROWOUT NVARCHAR(MAX) OUTPUT,#rowNum_IN INT''
While #rowNum <= #maxrows
BEGIN
SET #HTMLROWS=#HTMLROWS + ''<tr>''
OPEN col
FETCH NEXT FROM col INTO #ColumnName
IF #DEBUG=1 Print ''#ColumnName: '' + #ColumnName
WHILE ##FETCH_STATUS=0
BEGIN
--Get nth row from table
--SET #exec_str=''SELECT #ROWOUT=(select top 1 ['' + #ColumnName + ''] from (select top '' + cast(#rownum as varchar) + '' * from #CustomTable2HTML order by CustColHTML_ID ASC) xxx order by CustColHTML_ID DESC)''
SET #exec_str=''SELECT #ROWOUT=(select ['' + #ColumnName + ''] from #CustomTable2HTML where CustColHTML_ID=#rowNum_IN)''
IF #DEBUG=1 PRINT ''#exec_str: '' + #exec_str
EXEC sp_executesql
#exec_str,
#ParmDefinition,
#ROWOUT=#ROW OUTPUT,
#rowNum_IN=#rownum
IF #DEBUG=1 SELECT #ROW as ''#Row''
SET #HTMLROWS =#HTMLROWS + ''<td>'' + IsNull(#ROW,'''') + ''</td>''
FETCH NEXT FROM col INTO #ColumnName
END
CLOSE col
SET #rowNum=#rowNum +1
SET #HTMLROWS=#HTMLROWS + ''</tr>''
END
SET #OUTPUT=''''
IF #maxrows>0
SET #OUTPUT= ''<table ' + #TBL_STYLE + '>'' + #FIELDS + #HTMLROWS + ''</table>''
DEALLOCATE col
'
END
ELSE
BEGIN
--This is the SQL String for table columns to be aligned on
--the vertical. So we select a table column, and then iterate
--through all the rows for that column, this forming one row
--of our html table.
SET #exec_str= N'
DECLARE #exec_str NVARCHAR(MAX)
DECLARE #ParmDefinition NVARCHAR(500)
DECLARE #DEBUG INT
SET #DEBUG=0
IF #DEBUG=1 Print ''Table2HTML -Vertical alignment''
--Make a copy of the original table adding an indexing column.
--We need to add an index column to the table to facilitate sorting
--so we can maintain the original table order as we iterate through
--adding HTML tags to the table fields.
--
--New column called CustColHTML_ID (unlikely to be used by someone
--else!)
select CustColHTML_ID=0,* INTO #CustomTable2HTML FROM (' + #TSQL_QUERY + ') SUB
IF #DEBUG=1 PRINT ''CustomTable2HTMLv2: Modfied temporary table''
--Now alter the table to add the auto-incrementing index.
--This will facilitate row finding
DECLARE #COUNTER INT
SET #COUNTER=0
UPDATE #CustomTable2HTML SET #COUNTER = CustColHTML_ID=#COUNTER+1
-- #HTMLROWS will store all the rows in HTML format
-- #ROW will store each HTML row as fields on each row are iterated
-- through using dynamic SQL and a cursor
DECLARE #HTMLROWS NVARCHAR(MAX)
DECLARE #ROW NVARCHAR(MAX)
SET #HTMLROWS=''''
-- #ColumnName stores the column name as found by the table cursor
-- #maxrows is a count of the rows in the table
DECLARE #ColumnName NVARCHAR(500)
DECLARE #maxrows INT
--Find row count of our temporary table
--This is used here purely to see if we have any data to output
SELECT #maxrows=count(*) FROM #CustomTable2HTML
--Create a cursor which will iterate through all the column names
--in the temporary table (excepting the one we added above)
DECLARE col CURSOR FOR
SELECT name FROM tempdb.sys.Columns
WHERE object_id=object_id(''tempdb..#CustomTable2HTML'')
AND name not like ''CustColHTML_ID''
ORDER BY column_id ASC
--For each **HTML** row, we need to for each iterate through
--each table column as the outer loop.
--Once the column name is identified, we use Coalesc to
--combine all the column values into a single string.
SET #ParmDefinition=N''#COLOUT NVARCHAR(MAX) OUTPUT''
OPEN col
FETCH NEXT FROM col INTO #ColumnName
WHILE ##FETCH_STATUS=0
BEGIN
--Using current column name, grab all column values and
--combine into an HTML cell string using COALESCE
SET #ROW=''''
SET #exec_str='' SELECT #COLOUT=COALESCE(#COLOUT + ''''</td>'''','''''''') + ''''<td>'''' + Cast(IsNull(['' + #ColumnName + ''],'''''''') as nvarchar(max)) from #CustomTable2HTML ''
IF #DEBUG=1 PRINT ''#exec_str: '' + #exec_str
EXEC sp_executesql
#exec_str,
#ParmDefinition,
#COLOUT=#ROW OUTPUT
SET #HTMLROWS =#HTMLROWS + ''<tr>'' + ''<td>'' + #ColumnName + ''</td>'' + #ROW + ''</tr>''
IF #DEBUG=1 SELECT #ROW as ''Current Row''
IF #DEBUG=1 SELECT #HTMLROWS as ''HTML so far..''
FETCH NEXT FROM col INTO #ColumnName
END
CLOSE col
SET #OUTPUT=''''
IF #maxrows>0
SET #OUTPUT= ''<table ' + #TBL_STYLE + '>'' + #HTMLROWS + ''</table>''
DEALLOCATE col
'
END
DECLARE #ParamDefinition nvarchar(max)
SET #ParamDefinition=N'#OUTPUT NVARCHAR(MAX) OUTPUT'
--Execute Dynamic SQL. HTML table is stored in #OUTPUT
--which is passed back up (as it's a parameter to this SP)
EXEC sp_executesql #exec_str,
#ParamDefinition,
#OUTPUT=#OUTPUT OUTPUT
RETURN 1
Execute stored procedure:
DECLARE #HTML1 NVARCHAR(MAX)
DECLARE #HTML2 NVARCHAR(MAX)
EXEC dbo.CustomTable2HTMLv4 'select * from StarWars',#HTML1 OUTPUT,'class="horizontal"',0
EXEC dbo.CustomTable2HTMLv4 'select * from StarWars',#HTML2 OUTPUT,'class="vertical"',1
SELECT #HTML1+#HTML2

SQL Server : how to insert using variable

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)

Column Matching Data featch in SQL

DECLARE #ompid NVARCHAR(max)
DECLARE #Names VARCHAR(max)
SELECT #Names = COALESCE(#Names + ') as ' + Variable_Name + ' ,AVG(', 'AVG(') + Variable_Name
FROM charttest
WHERE ompid = 125
DECLARE #lastcol NVARCHAR(100) = (
SELECT TOP 1 (Variable_Name)
FROM charttest
WHERE ompid = 125
ORDER BY Variable_Name ASC
)
DECLARE #Names2 NVARCHAR(500) = #Names + ') as ' + #lastcol + ''
DECLARE #sql NVARCHAR(500)
SET #sql = 'SELECT ' + #Names2 + ' FROM ompvaribles'
EXEC (#sql)
This is my sql query i had show second table avg but not getting second table records.
I'm not sure what is actually not working with your query (I'm assuming you get an error), but in order to execute the dynamic query you've build you need to use:
exec sp_executesql #sql

where clause to search values from multiple column in sql server

I have the sql server table with 51 columns like below
id
remarks1
remarks2
.
.
.
remarks50
I need to search if particular string is present in atleast one remarks field like in the example below
id remarks1 remarks2 remarks3 remarks4
1 key nonkey grabaze jjjjj
2 uuu 888 8888 kkk
3 888 key hjhj kjkj
suppose i need to search key which is present in either remarks1,2,3.....or 50
I can have sql like
select id from tbl where remarks1 ='key' or remarks2='key' and so on ..
writing or query upto 50 columns is really unpractical.. do we have any quick method?
You can try using below stored procedure .
CREATE PROCEDURE sp_FindStringInTable #stringToFind VARCHAR(100), #schema sysname, #table sysname
AS
DECLARE #sqlCommand VARCHAR(8000)
DECLARE #where VARCHAR(8000)
DECLARE #columnName sysname
DECLARE #cursor VARCHAR(8000)
BEGIN TRY
SET #sqlCommand = 'SELECT * FROM [' + #schema + '].[' + #table + '] WHERE'
SET #where = ''
SET #cursor = 'DECLARE col_cursor CURSOR FOR SELECT COLUMN_NAME
FROM ' + DB_NAME() + '.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = ''' + #schema + '''
AND TABLE_NAME = ''' + #table + '''
AND DATA_TYPE IN (''char'',''nchar'',''ntext'',''nvarchar'',''text'',''varchar'')'
EXEC (#cursor)
OPEN col_cursor
FETCH NEXT FROM col_cursor INTO #columnName
WHILE ##FETCH_STATUS = 0
BEGIN
IF #where <> ''
SET #where = #where + ' OR'
SET #where = #where + ' [' + #columnName + '] LIKE ''' + #stringToFind + ''''
FETCH NEXT FROM col_cursor INTO #columnName
END
CLOSE col_cursor
DEALLOCATE col_cursor
SET #sqlCommand = #sqlCommand + #where
--PRINT #sqlCommand
EXEC (#sqlCommand)
END TRY
BEGIN CATCH
PRINT 'There was an error. Check to make sure object exists.'
IF CURSOR_STATUS('variable', 'col_cursor') <> -3
BEGIN
CLOSE col_cursor
DEALLOCATE col_cursor
END
END CATCH
The stored procedure gets created in the master database so you can use it in any of your databases and it takes three parameters:
stringToFind - this is the string you are looking for. This could be a simple value as 'test' or you can also use the % wildcard such as '%test%', '%test' or 'test%'.
schema - this is the schema owner of the object
table - this is the table name you want to search, the procedure will search all char, nchar, ntext, nvarchar, text and varchar columns in the table
Source
You can use an unpivot to transpose the remarks* columns as rows with a common column name, which you can then filter on. (You'll need to repeat all 51 columns).
Distinct will be needed to eliminate cases where more than one column matches (i.e. to mimic the original or)
SELECT DISTINCT ID, Rmk
FROM
(SELECT ID, Remarks1, Remarks2, Remarks3, Remarks4
FROM Remarks) r
UNPIVOT
(Rmk FOR RmkCol IN (Remarks1, Remarks2, Remarks3, Remarks4))AS unpvt
WHERE rmk = 'key';
Sql Fiddle here
However I would advise you to reconsider normalising this into a 1 to many Remarks table - if your table is large, you will need a large number of indexes on the Remark* columns.
SELECT remarks1, remarks2, remarks3, remarks4 from tabl_name
WHERE CONTAINS (( remarks1, remarks2, remarks3, remarks4),'888') ORDER BY id;

Creating A Script To Replicate A Table And Its Contents?

I know you can create a script to replicate a table using:
right click table > script table as > create to > new query editor window
But how can I generate a script that contains a bunch of insert commands for each row in the table?
Table1
Id1, Row1
Id2, Row2
Id3, Row3
Insert into Table1 values(Row1);
Insert into Table1 values(Row2);
Insert into Table1 values(Row3);
I ended up doing this
right click database > Tasks > Generate Scripts ... > selected the tables > in the advanced options I set "Types of data to script" to "Schema and data"
Select
'Insert into Table (
IntField1
StringField2
Column3)
values (' +
IntField1 + ',' +
+ '''' + StringField2 + ''',' +
Column2 + ')' as InsertQuery
From Table
Something like this, just remember if your string contains a single quote you will need to make sure you replace it like this replace(stringfield, '''', '''''')
So this isnt super pretty cuz I kind of took one of my sp's and hacked it up for this. But basically this will take any table and print a series of insert statements into a table called tbl_text (which you would need to create)
The arguments are the table name and the table ID from sysobjects
--this is how you get the tbl_id
SELECT id FROM sysobjects WHERE type = 'U' AND name = 'tablename'
CREATE PROCEDURE dbo.sp_export_table
#tblhdr varchar(100),
#tblID varchar(100)
AS
SET NOCOUNT ON
IF object_id('tempdb..##temptable') IS NOT NULL
BEGIN
DROP TABLE ##temptable
END
DECLARE #identity bit
DECLARE #typestmt nvarchar(100)
DECLARE #typeval int
DECLARE #rowstmt nvarchar(1000)
DECLARE #rowID varchar(50)
DECLARE #orderby nvarchar(100)
DECLARE #clmnstmt varchar(200)
DECLARE #clmnhdr varchar(50)
DECLARE #clmnstring varchar(1000)
DECLARE #valuestmt nvarchar(200)
DECLARE #valuestring nvarchar(3000)
DECLARE #value nvarchar(1000)
DECLARE #insertstmt varchar(1000)
DECLARE #params nvarchar(100)
DECLARE #param2 nvarchar(100)
SELECT #rowstmt = N'SELECT TOP 1 #inside_var = name FROM syscolumns WHERE id = ' + #tblID + ' ORDER BY colorder'
SELECT #params = N'#inside_var NVARCHAR(1000) OUTPUT'
EXEC sp_executesql #rowstmt, #params, #inside_var = #orderby OUTPUT
SELECT #rowstmt = 'SELECT *, ROW_NUMBER() OVER (ORDER BY ' + #orderby + ') AS row INTO ##temptable FROM ' + #tblhdr
exec(#rowstmt)
IF object_id('tempdb..##temptable') IS NOT NULL
BEGIN
DECLARE row_cursor CURSOR FOR
SELECT row FROM ##temptable
OPEN row_cursor
FETCH NEXT FROM row_cursor
INTO #rowID
--if table has identity and has records write identity_insert on
SET #identity = 0
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE OBJECTPROPERTY(OBJECT_ID(TABLE_NAME),
'TableHasIdentity') = 1 AND TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME = #tblhdr) AND EXISTS(SELECT * FROM ##temptable)
BEGIN
SET #identity = 1
INSERT INTO dbo.tbl_text VALUES('SET IDENTITY_INSERT dbo.' + #tblhdr + ' ON')
END
WHILE ##FETCH_STATUS = 0
BEGIN
SELECT #clmnstmt = 'DECLARE column_cursor CURSOR FOR SELECT name FROM syscolumns WHERE id = ' + #tblID + ' ORDER BY colorder'
exec(#clmnstmt)
OPEN column_cursor
FETCH NEXT FROM column_cursor
INTO #clmnhdr
SELECT #clmnstring = '('
SELECT #valuestring = '('
WHILE ##FETCH_STATUS = 0
BEGIN
IF #clmnhdr <> 'row'
BEGIN
SELECT #clmnstring = #clmnstring + #clmnhdr + ','
SELECT #valuestmt = N'SELECT #inside_var = ' + #clmnhdr + ' FROM ##temptable WHERE row = ' + #rowID
EXEC sp_executesql #valuestmt, #params, #inside_var = #value OUTPUT
SELECT #typestmt = N'SELECT #inside_var2 = xtype FROM syscolumns WHERE name = ''' + #clmnhdr + ''' AND id = ' + #tblID
SELECT #param2 = N'#inside_var2 INT OUTPUT'
EXEC sp_executesql #typestmt, #param2, #inside_var2 = #typeval OUTPUT
IF #typeval NOT IN (48,52,56,59,60,62,104,108,122,127)
BEGIN
SET #value = REPLACE(#value,'''','''''')
SET #value = '''' + #value + ''''
SET #value = ISNULL(#value, '''''')
END
IF NOT (#typeval = 34)
BEGIN
SELECT #valuestring = #valuestring + #value + ','
END
ELSE
BEGIN
SELECT #valuestring = #valuestring + '''''' + ','
END
END
FETCH NEXT FROM column_cursor
INTO #clmnhdr
END
SET #clmnstring = LEFT(#clmnstring, LEN(#clmnstring) - 1)
SET #valuestring = LEFT(#valuestring, LEN(#valuestring) - 1)
INSERT INTO dbo.tbl_text VALUES('INSERT INTO dbo.' + #tblhdr + ' ' + #clmnstring + ') VALUES' + #valuestring + ')')
FETCH NEXT FROM row_cursor
INTO #rowID
CLOSE column_cursor
DEALLOCATE column_cursor
END
--if it wrote identity_insert on, turn it off
IF (#identity = 1)
BEGIN
INSERT INTO dbo.tbl_text VALUES('SET IDENTITY_INSERT dbo.' + #tblhdr + ' OFF')
END
CLOSE row_cursor
DEALLOCATE row_cursor
END
IF object_id('tempdb..##temptable') IS NOT NULL
BEGIN
DROP TABLE ##temptable
END
GO
If you've got an account on SSC, you can use the script I published last year. It works without cursors an it enables custom filtering.
http://www.sqlservercentral.com/scripts/Script+Data/65998/
Hope this helps
Assuming Row is an INT NOT NULL. You could write a SELECT statement that outputs SQL;
SELECT N'INSERT INTO Table1 VALUES (' + CAST(Row AS NVARCHAR(10)) + N');'
FROM Table1
Then output your results to text.