Export query to text file - tsql

What I am trying to do is export a Tsql query to a csv file. Simple enough, however I need to be able to specify which fields are wrapped in quotes "". I can get my query to export with all the feilds wrapped.
"SCHEN001","Joe Bloggs Inc","1","1","1","1","1","1","13","6","Mr John Smith"
What I would like to export is
"SCHEN001","Joe Bloggs Inc",1,1,1,1,1,1,13,6,"Mr John Smith"
Is this possible using Tsql?
Any ideas would be greatly appreciated

Take a look at the bcp.exe utility. It is ment for bulk operations and you can specify templates for exports etc.
A link that seems reasonable: http://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/

Another approach is to use SQL Server Integration Services (if you have MS SQL Server Standard or Enterprise edition)
or, alternatively, you can copy grid results into Excel, and export CSV from there :-)

Try to use this script.
Set variable #TblName to the name of your table.
The script uses information_schema.columns
to get the datatypes for every column in selected table.
DECLARE #TblName varchar(128)
DECLARE #WhereClause varchar(255)
DECLARE #cmd varchar(7000)
SET #TblName = '<YOUR TABLENAME>' --TABLENAME
SET #cmd = ''
create table #tableDef (id int identity (1,1), ColType int, ColName varchar(128))
--Fetch table information
insert #tableDef (ColType, ColName)
select case when DATA_TYPE like '%char%' then 1
when DATA_TYPE like '%datetime%' then 2
else 0 end ,
COLUMN_NAME
from information_schema.columns
where TABLE_NAME = #TblName
order by ORDINAL_POSITION
SELECT #cmd = #cmd
+ ' CASE WHEN ' + ColName + ' IS NULL '
+ ' THEN ''NULL'' '
+ ' ELSE '
+ case ColType
when 1 then ''''''''' + ' + ColName + ' + '''''''''
when 2 then ''''''''' + ' + 'CONVERT(VARCHAR(20),' + ColName + ')' + ' + '''''''''
else 'CONVERT(VARCHAR(20),' + ColName + ')' end
+ ' END + '','' + '
from #tableDef
order by id
select #cmd = 'SELECT ' + left(#cmd,len(#cmd)-8) + '+'''' FROM ' + #tblName
exec (#cmd)
drop table #tableDef

Related

Loop through each table in database and get first three digits of one column

I am trying to create a table that will show me each table name, and the first three characters of the ID column (every table has this column), and then put that data into a table. We are using this to help map dependencies in our Salesforce Org which is replicated onto SQL using dbAMP. I adapted the code below as far as I could, and am looking for help to finish it.
UPDATE: The first half of the question is resolved, and the code now runs to give the first three characters of the ID. I still could use help in converting this code to spool the results into one single table.
USE Salesforce
GO
IF OBJECT_ID('tempdb..#tempResults') IS NOT NULL
DROP TABLE #tempResults
CREATE TABLE #tempResults
(
[Object_ID] VARCHAR(3)
--, [org] VARCHAR(100)
, [Table_Name] VARCHAR(100)
)
DECLARE cur CURSOR FOR
SELECT
'SELECT DISTINCT LEFT(' + QUOTENAME(c.COLUMN_NAME) + ',3) AS
[Object_ID], '''
--+ QUOTENAME(TABLE_CATALOG) + ' as [Org], '
+ QUOTENAME(TABLE_NAME) + ''' as [Table_Name] FROM '
+ QUOTENAME(TABLE_CATALOG) + '.' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
FROM INFORMATION_SCHEMA.COLUMNS AS c
WHERE
c.[DATA_TYPE] IN ('nchar','varchar', 'nvarchar')
AND c.[CHARACTER_MAXIMUM_LENGTH] = 18
and c.TABLE_NAME not like '%upload%'
and c.TABLE_NAME not like '%Delta%'
and c.TABLE_NAME not like '%Update%'
and c.TABLE_NAME not like '%Previous%'
and C.COLUMN_NAME = 'ID'
DECLARE #cmd VARCHAR(MAX);
OPEN cur;
FETCH NEXT FROM cur INTO #cmd;
WHILE ##FETCH_STATUS = 0
BEGIN
--PRINT #cmd
INSERT INTO #tempResults
EXEC(#cmd);
FETCH NEXT FROM cur INTO #cmd;
END
CLOSE cur;
DEALLOCATE cur;
SELECT * FROM #tempResults
I'm not getting the Column ID trimmed to the first 3 characters, and it's not outputting to a table. I'm not that familiar with cursors so I'd appreciate any help. Thanks,
You're really close. The way you've got your select written it's actually trimming the column name and then appending that 3-character column name to the string, rather than getting the first three characters from the actual data within the ID column.
Try updating your select so the LEFT brackets the QUOTENAME(c.COLUMN_NAME) inside the string, like below. This small change made your script work on my salesforce installation.
SELECT
'SELECT DISTINCT LEFT(' + QUOTENAME(c.COLUMN_NAME) + ',3) AS '
+ QUOTENAME(TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME) + ' FROM '
+ QUOTENAME(TABLE_CATALOG) + '.' + QUOTENAME(TABLE_SCHEMA) + '.'
+ QUOTENAME(TABLE_NAME)
FROM INFORMATION_SCHEMA.COLUMNS AS c
WHERE
c.[DATA_TYPE] IN ('nchar','varchar', 'nvarchar')
AND c.[CHARACTER_MAXIMUM_LENGTH] = 18
and c.TABLE_NAME not like '%upload%'
and c.TABLE_NAME not like '%Delta%'
and c.TABLE_NAME not like '%UPdate%'
and c.TABLE_NAME not like '%Previous%'
and C.COLUMN_NAME = 'ID'
Update to answer part two:
First you'll need a destination table - I've used a temporary table, but this will work just fine as a permanent table.
I've adjusted the code to output two columns instead of one: IDSubstring (your 3-character ID portion) and SourceTable (this is exactly the same information that you were using as a column name previously). This way we know which table the ID portion belongs to.
Then, inside the cursor, instead of just executing, we do this:
INSERT INTO #tempResults
EXEC(#cmd);
This will populate our table and give us selectable values.
CREATE TABLE #tempResults
(
[IDSubstring] VARCHAR(3)
, [SourceTable] VARCHAR(50)
)
DECLARE cur CURSOR FOR
SELECT
'SELECT DISTINCT LEFT(' + QUOTENAME(c.COLUMN_NAME) + ',3) AS [IDSubstring], '''
+ QUOTENAME(TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME) + ''' as [SourceTable] FROM '
+ QUOTENAME(TABLE_CATALOG) + '.' + QUOTENAME(TABLE_SCHEMA) + '.'
+ QUOTENAME(TABLE_NAME)
FROM INFORMATION_SCHEMA.COLUMNS AS c
WHERE
c.[DATA_TYPE] IN ('nchar','varchar', 'nvarchar')
AND c.[CHARACTER_MAXIMUM_LENGTH] = 18
and c.TABLE_NAME not like '%upload%'
and c.TABLE_NAME not like '%Delta%'
and c.TABLE_NAME not like '%UPdate%'
and c.TABLE_NAME not like '%Previous%'
and C.COLUMN_NAME = 'ID'
DECLARE #cmd VARCHAR(MAX);
OPEN cur;
FETCH NEXT FROM cur INTO #cmd;
WHILE ##FETCH_STATUS = 0
BEGIN
--PRINT #cmd
INSERT INTO #tempResults
EXEC(#cmd);
FETCH NEXT FROM cur INTO #cmd;
END
CLOSE cur;
DEALLOCATE cur;
SELECT * FROM #tempResults
Update 2
You're correct in assuming it's an issue with the '''
The ''' is there because we need to build our string with single quotes inside of it.
As examples:
SELECT '' returns nothing
while SELECT '''' returns '
And SELECT 'This is an example''' will return This is an example'
So the ''' are part of a larger string definition started with the initial ' before "This" and can be broken down this way - the first two single quotes are the single quote we want to print within the string and the third single quote is the string terminating quote. If you just run the select statement and look at what it outputs, you can see where each single quote has been inserted into the string.
Updated SELECT is below.
SELECT
'SELECT DISTINCT LEFT(' + QUOTENAME(c.COLUMN_NAME) + ',3) AS [Object_ID], '''
+ QUOTENAME(TABLE_CATALOG) + ''' as [Org], '''
+ QUOTENAME(TABLE_NAME) + ''' as [Table_Name] FROM '
+ QUOTENAME(TABLE_CATALOG) + '.' + QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
FROM INFORMATION_SCHEMA.COLUMNS AS c
WHERE
c.[DATA_TYPE] IN ('nchar','varchar', 'nvarchar')
AND c.[CHARACTER_MAXIMUM_LENGTH] = 18
and c.TABLE_NAME not like '%upload%'
and c.TABLE_NAME not like '%Delta%'
and c.TABLE_NAME not like '%Update%'
and c.TABLE_NAME not like '%Previous%'
and C.COLUMN_NAME = 'ID'

Cannot cast int to varchar in STUFF while joining select

I am using stuff to join results of select. The columns I need to join are not constant and could change. So I put them in a variable and prepare a dynamic query.
SET #sql = N'Select #newvalues = STUFF( ( Select '','' + ' + #columns + ' FROM #MYINSERTED FOR XML PATH(''''),TYPE)
.value(''.'',''VARCHAR(MAX)''),1,2,'''')'
This query transforms into what looks like follows:
Select #newvalues = STUFF(
( Select ',' + ID, CaseID, DocumentType, FileName, FileExtension, FilePath, InsertDate, InsertedBy, ModifiedDate, ModifiedBy, OriginalFileName
FROM #MYINSERTED FOR XML PATH(''),TYPE)
.value(''.'',''VARCHAR(MAX)''),1,2,'')
But the first column ID is an integer and I get the following error.
Conversion failed when converting the varchar value ',' to data type int.
Please guide as to if there could be any workaround and I cannot cast each column individually as columns could change.
No reason to use STUFF, FOR XML PATH, etc. to merge the output of all columns into a single string. Simply make sure that all columns are explicitly cast when you construct your dynamic SQL.
How do you construct your #columns variable? I assume it contains something like "ID, CaseID, DocumentType, ...". You must construct a similar variable that does the explicit casting, or transform your #columns variable like this:
SET #columnsCast = 'CAST(' + REPLACE(#columns, ',', ' AS VARCHAR(MAX)) + '', '' CAST( ') + ' AS VARCHAR(MAX))'
This should make the #columnsCast variable look like this:
CAST(ID AS VARCHAR(MAX)) + ', ' + CAST(CaseID AS VARCHAR(MAX)) + ', ' + CAST(DocumentType AS VARCHAR(MAX)) + ', ' ...
Then simply perform a dynamic SQL statement like this:
SET #sql = N'DECLARE #newvalues AS VARCHAR(MAX) = '''';
SELECT #newvalues = #newvalues + ' + #columnsCast + ' FROM #MYINSERTED'
Then when you execute this expression, make sure to output #newvalues like this:
DECLARE #newvals AS VARCHAR(MAX)
EXECUTE sp_executesql #sql, N'#newvalues VARCHAR(MAX) OUTPUT', #newvalues=#newvals OUTPUT
SELECT #newvals

How can I convert SQL Server data to importable goinstant data?

I'm looking for a way to import a SQL Server table data into goinstant. Is there a JSON editor or script that allows this and that can be clipboard pasted right into goinstant?
I may have come up with a solution. I was able to use a previously created sql to json object tsql script written by Matthew D. Erwin. I modified it to handle creating an importable output to goinstant. See the script here on github, or feel free to grab it from below.
/****** Object: StoredProcedure [dbo].[GetJSON] Script Date: 5/16/2014 9:04:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--
-- Author: Matthew D. Erwin (Snaptech, LLC)
-- Create date: May 9, 2013
-- Modified date: May 16, 2014 by Rudy E. Hinojosa (CBM Archives, LLC) rudy.hinojosa#cbmarchives.com
-- Description: Returns the contents of a given table
-- in JavaScript Object Notation JSON -
--
-- Very notably useful for generating MOCK .json files
-- for testing or before RESTful services are completed.
--
-- This implementation:
-- *removed cursor (using FOR XML PATH(''))
-- *properly supports NULL vs quoted values
-- *supports dates in ISO 8601 - presuming UTC
-- *uses Data_Type and Is_Nullable info
-- *escapes '\'
-- *formats output with tabs/newlines
-- *can return final results as XML to bypass
-- truncation in SSMS
-- *supports schema (e.g. [dbo].[TableName]
-- *includes "recordCount" field
-- Options:
-- #table_name: the table to execute the query
-- #limit: equivalent to "select top N * from table"
-- #ssms: flag to use if executing in Sql Server Management Studio
-- to bypass result truncation limits.
-- #isgoinstant: flag to use if importing results to GoInstant database
--
-- Inspired primarily by the 2008 work of Thiago R. Santos which was influenced by Thomas Frank.
-- Usage: [dbo].[GetJSON] #Table_name = 'MySchema.MyTable', #limit = 50, #ssms = 0, #isgoinstant = 0
CREATE procedure [dbo].[GetJSON] (
#table_name varchar(max),
#limit int = null,
#ssms bit = 0,
#isgoinstant bit = 0
)
as
begin
declare #json varchar(max), #query varchar(max), #table_schema varchar(max) = null
if( charindex('.', #table_name) > 0 )
begin
set #table_schema = replace(replace( substring(#table_name, 0, charindex('.',#table_name)), '[', ''), ']', '')
set #table_name = replace(replace( substring(#table_name, charindex('.',#table_name) + 1,len(#table_name)), '[', ''), ']', '')
end
set #query =
'select ' + case when #limit is not null then 'top ' + cast(#limit as varchar(32)) + ' ' else '' end + ''' '' + REVERSE(STUFF(REVERSE(''' +
--case when #isgoinstant is not null then ('"' + cast(newid() as varchar(max)) + '"' + ': { ') else '' end +
CAST((SELECT ' "' + column_name + '" : ' +
case when is_nullable = 'YES'
then ''' + case when [' + column_name + '] is null then ''null'' else ' +
case when data_type like '%binar%' then null else '' end +
case when data_type like 'XM%' then null else '' end +
case when data_type like '%char%' or data_type like '%text%' then '''"'' + ' else '' end +
case when data_type like '%date%' then 'char(34) + convert(varchar(23),[' + column_name + '], 126) + ''Z'' + char(34)' else
'replace(replace(replace(replace(cast([' + column_name + '] as varchar(max)),''\'',''\\''),''"'',''\"''),char(10),''\n''),char(13),''\n'') ' end +
case when data_type like '%char%' or data_type like '%text%' then '+ ''"''' else '' end + ' end + '''
else
case when data_type like '%binar%' then null else '' end +
case when data_type like 'XM%' then null else '' end +
case when data_type like '%char%' or data_type like '%text%' then '"' else '' end +
''' + ' +
case when data_type like '%date%' then 'char(34) + convert(varchar(23),[' + column_name + '], 126) + ''Z + char(34)' else
'replace(replace(replace(replace(cast([' + column_name + '] as varchar(max)),''\'',''\\''),''"'',''\"''),char(10),''\n''),char(13),''\n'') + ''' end +
case when data_type like '%char%' or data_type like '%text%' then '"' else '' end end + ',' AS [text()]
from information_schema.columns where table_name = #table_name and (#table_schema is null or table_schema = #table_schema) FOR XML PATH('') ) as varchar(max)) +
'''),1,1,'''')) + '' }'' as json into tmpJsonTable from ' + #table_name + ' with(nolock) '
exec sp_sqlexec #query
if (#isgoinstant = 0)
set #json =
'{' + char(10) + char(9) +
'"recordCount" : ' + Cast((select count(*) from tmpJsonTable) as varchar(32)) + ',' + char(10) + char(9) +
'"records" : ' + char(10) + char(9) + char(9) + '[' + char(10)
+ REVERSE(STUFF(REVERSE(CAST((SELECT char(9) + char(9) + json + ',' + char(10) AS [text()] FROM tmpJsonTable FOR XML PATH('')) AS varchar(max))),1,2,''))
+ char(10) + char(9) + char(9) + ']' + char(10) + '}'
else
set #json =
'{' + char(10) + char(9) +
REVERSE(STUFF(REVERSE(CAST((SELECT case when #isgoinstant is not null then ('"' + cast(newid() as varchar(max)) + '"' + ': { ') else '' end + char(9) + json + ',' + char(10) AS [text()] FROM tmpJsonTable FOR XML PATH('')) AS varchar(max))),1,2,''))
+ char(10) + char(9) + char(9) + '}'
drop table tmpJsonTable
if( #ssms = 1 and len(#json) > 65535 ) --deal with Sql Server Management Studio text/grid truncation
select cast('<json><![CDATA[' + #json + ']]></json>' as xml) as jsonString
else
select #json as jsonString
end
GO

the multi-part identifier "#tmpFullname.Item" could not be bound."

The following code is supposed to take a string that may or may not be comma delimited and put it into a table (#tmpFullanme) that part works flawlessly. The second part is supposed return all the values that are LIKE / NOT LIKE with or without % symbols based on what is input. The error that I am getting is "the multi-part identifier "#tmpFullname.Item" could not be bound." The best guess I have is that it may be out of scope?
DROP PROCEDURE uspJudgments;
GO
CREATE PROCEDURE uspJudgments
#fullName varchar(100), #SrchCriteria1 varchar(15), #SrchCriteria2 varchar(15), #qualifier varchar(10)
AS
BEGIN
SELECT *
INTO #tmpFullname
FROM dbo.DelimitedSplit8K(#fullName, ',')
DECLARE #Query NVarChar(1024)
SET #Query = 'SELECT d.*' + ' FROM defendants_ALL d, #tmpFullname' +
' WHERE d.combined_name' + ' ' + #qualifier + ' ' + '''' + #SrchCriteria1 + '''' + ' + ' + '''' + #tmpFullname.Item + '''' + ' + ' + '''' + #SrchCriteria2 + ''''
END
EXEC sp_executesql #Query
PRINT(#Query)
IF OBJECT_ID('#tmpFullname', 'U') IS NOT NULL
DROP TABLE #tmpFullname
EXEC uspJudgments #qualifier = 'LIKE', #fullName = 'johnson', #SrchCriteria1 = '%', #SrchCriteria2 = '%'
Cannot get to the PRINT output as "the multi-part identifier "#tmpFullname.Item" could not be bound." If I change #tmpFullname.Item to '#tmpFullname.Item it goes through and returns nothing but it shows that the query is correct minus the issue with that table.
SELECT d.* FROM defendants_ALL d, #tmpFullname WHERE d.combined_name LIKE '%' + '#tmpFullname.Item' + '%'
Please note that until I made this into a dynamic query so I can change the statement from LIKE to IN etc it worked very well.
I set up a full test to get the proper script to get you your desired results. I also have a SQL Fiddle showing how this works. Note You will want to run EXECUTE sp_executesql #Query inside the stored procedure
ALTER PROCEDURE uspJudgments #fullName varchar(100)
, #SrchCriteria1 varchar(15)
, #SrchCriteria2 varchar(15)
, #qualifier varchar(10)
AS
BEGIN
--Simulates your split function
SELECT *
INTO #tmpFullName
FROM
(
SELECT 'firstTest' AS Item
UNION ALL SELECT 'secondTest'
UNION ALL SELECT 'NotThere'
) AS t;
DECLARE #Query NVARCHAR(1024);
SELECT #Query = 'SELECT d.* '
+ ' FROM defendants_ALL d '
+ ' CROSS JOIN #tmpFullName AS t '
+ ' WHERE d.combined_name' + ' ' + #qualifier + ' '
+ '''' + #SrchCriteria1 + ''''
+ ' + ' + 't.Item' + ' + ' + '''' + #SrchCriteria2 + '''';
EXECUTE sp_executesql #Query;
END
EXECUTE uspJudgments
#fullName = 'does not matter'
, #SrchCriteria1 = '%'
, #SrchCriteria2 = '%'
, #qualifier = 'LIKE';
You have to use the tempdb prefix in this case
insert into tempdb..#TABLENAME
and
set #query = 'select * from tempdb..#TABLENAME'
Well, After my last answer i have found several things..
When i look into your procedure you start with the "BEGIN", next you do a insert into the "#tmpFullName" table, your declare the "#Query" variable and create a select statement.
After that you do and "END" with logic after it. You do the "sp_executesql" after that you drop the temptable and you do and EXEC of the current procedure..
The Structure isn't all that readable, sorry to tell you. So maybe you go there first.
Beside a strange structure you are using the "#tmpFullName.Item" in some dynamic SQL as a parameter, while it is declared inside the SQL query it self. So you have to do something like this :
SET #Query = 'SELECT d.*' + ' FROM defendants_ALL d, #tmpFullname' +
' WHERE d.combined_name' + ' ' + #qualifier + ' ' + '''' + #SrchCriteria1 + '''' + ' + ' + ' #tmpFullname.Item ' + ' + ' + '''' + #SrchCriteria2 + ''''
Where the "#tmpFullName.Item" resides insde the code, not as a parameter. But then again, what are you trying to achieve here? To answer this completly we have to know what the other variables are. Your structure isn't telling me what are trying to achieve..
I really can't make any more out of it...

Applying T-SQL...Back to Basics

IM very new to TSQL and am getting my head around stored proceedures etc.
I am using the Code to find a value within one table in my data base, but im not too sure how i would use this code...
Do i need to replace all #* with my relevant table or column name or simply compy paste and execute
Thanks for the help
How do I find a value anywhere in a SQL Server Database?
CREATE PROC SearchAllTables
(#SearchStr nvarchar(100) ) AS BEGIN
-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
-- Purpose: To search all columns of all tables for a given search string
-- Written by: Narayana Vyas Kondreddi
-- Site: http://vyaskn.tripod.com
-- Tested on: SQL Server 7.0 and SQL Server 2000
-- Date modified: 28th July 2002 22:50 GMT
DECLARE #Results
TABLE(ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #SearchStr2 nvarchar(110)
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
SET #TableName = (SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > #TableName
AND OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0)
WHILE (#TableName IS NOT NULL)
AND (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =(SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(#TableName, 2)
AND TABLE_NAME = PARSENAME(#TableName, 1)
AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > #ColumnName)
IF #ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
('SELECT ''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630)
FROM ' + #TableName + ' (NOLOCK) ' + ' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2)
END
END
END
SELECT ColumnName, ColumnValue
FROM #Results
END
Well, to answer your question:
First you have to copy the code to a query window an run it.
That will create the stored procedure.
Now you can call the stored procedute by calling:
EXEC SearchAllTables 'a string of your choice'
Note that you will only get hits from text columns (like 'char', 'varchar', 'nchar', 'nvarchar').