Stored procedure results in CSV format - tsql

I have a stored procedure that does a select and returns the results. I would like thos results to be in .csv format. NOT a csv file, a string in csv format. Also, I am not looking for any menu clicks or selections from management studio. This must be done inside the stored procedure call. What is the correct way to do this?

This might work for you. Column headers is optional:
select 'NVarCharColumn1Header,IntColumn2Header'
+ char(13) + char(10)
+ (select NVarCharColumn1
+ ',' + cast(IntColumn2 as nvarchar)
+ char(13) + char(10)
from Table1
for xml path(''), type).value('(./text())[1]','nvarchar(max)')

Related

ADF dynamic content using concat - need to embed commas inside of string for long list of columns

The use case seems pretty simple....
Produce a sql statement as part of a copy activity that includes a hard coded column listing and also concatenated to a parameter-provided database and table name (since the database and table names can change across environments such as dev/test/prod).
The problem is....If you use concat function it treats every comma as a new value to be concatenated. I was hoping for a way to escape the comma and treat it as a value but nothing I've tried works.
For example....concatenate the following string ....
SELECT event_date, event_timestamp,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_title') AS page_title
FROM '
to... pipeline().parameters.Database_Nm + . + pipeline().parameters.Table_Nm
The workaround has been to quote the beginning and end of every line so the comma is treated as data so every column/line is a separate concatenation such as this....
#concat('SELECT event_date,',
'(SELECT value.string_value FROM UNNEST(event_params) WHERE key = ''page_title'') AS page_title,',
'from ', pipeline().parameters.Database_Nm, '.', pipeline().parameters.Table_Nm
That works...but I have over a hundred columns so this is just a bit silly as a solution. Am I missing a simpler method? TIA!
When most of your string is hard coded and not expressions you can use the following string interpolation expression format:
SELECT event_date,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_title') AS page_title
from #{pipeline().parameters.Database_Nm}.#{pipeline().parameters.Table_Nm}

Add data to DB2 through LotusScript

I need to add data to a DB2 database through LotusScript from the WebQueryOpen of a webpage. I have the agent opening the connection and I understand that I need to use an INSERT INTO command, I just am not sure of the actual syntax I need to use to accomplish the INSERT. What do I need to know?
You'll need to generate SQL that looks something like this:
INSERT INTO YourTable (columnName1, columname2, etc )
VALUES ('value 1', 'value 2', etc);
Typically, in LotusScript, you would be doing this with something like this:
Dim sql as string
sql = "INSERT INTO MyTable (columnName1, columname2) VALUES ('" + doc.field1(0) + "', '" + doc.field2(0) + "');"

finding length of longest coulmn in a database - SELECT subquery

My skills in SQL are limited:
I have a database (SQLBase in this case) that has a couple of LONGVARs in columns.
I'm searching for the actual length of all COLUMNS that have a particular type.
SELECT tbname,name FROM sysadm.syscolumns where coltype='LONGVAR';
The above statement works. It gives me all tables and the respective column names that have a LONGVAR datatype. Now I would like to take these data and search through all the respective tables (the rows, so the data) and find the lengths of the respective LONGVAR columns (to find the maximum for instance, or those above a certain limit).
I have the idea that it can be solved with a subquery of nested SELECT statement but have no idea how to formulate the statement.
I don't have any real knowledge of SQLbase, so I may be off-base here: but if I was trying to do this on SQL Server, a simple approach would be to do something like the following:
SELECT
tbname,
name,
'SELECT ''' + tbname + ''' AS TableName, ''' + name + ''' AS ColumnName, MAX(LEN(' + name + ')) AS ColumnLength FROM ' + tbname + ' -- add a WHERE clause here if needed' AS Query
FROM sysadm.syscolumns
WHERE coltype='LONGVAR';
This will output a set of values, which you could then copy/paste into a new query editor window and examine before running.
Other, more complex solutions would involve dynamic SQL that automatically executes each of these statements; but again, not knowing much about SQLbase, this is where I would start.

SQL Server openrowset() test column count in MS Access table

Haven't found an answer via Google. I need to execute this code from SQL Server stored proc.
I have a folder with 100+ access dbs with a table called tblReports. Some of the access db's have an extra column in tblReports called AdminReport.
I need to capture the extra column if it exists, thus... I need to test how many columns are in tblReports so that I can use an if/else statement in the sp to generate the correct sql based on the column count.
I'd love to read your thoughts, here's the relevant snippet.
set #sql = 'Insert into CustomerServiceIntranet.dbo.ReportCriteria
(UserInfo,RptNbr,RptType,RptDesc,GroupCDBrk,ClientCDBrk,CategoryCDBrk,
UserIDBrk,UnitCDBrk,WrkTypeBrk,StatCDBrk,StatDatBrk,
ExperBrk,GroupList,ClientList,CategoryList,UserIDList,BusAreaList,
WrkTypList,StatusList,QueueList,ReviewDay,ReviewDayNA,
ErrorImpact,DateRange,DataSource,RptPathFile)'
+ 'Select '''+ #userfilename + ''', ors.* '
+ 'from (select * From Openrowset(''Microsoft.ACE.OLEDB.12.0'','''
+ #CurrentName
+ ''';''Admin'';,''select * from tblReports'')) ors'
The standard approach would be to link to tblReports by calling DoCmd.TransferDatabase. You would then be able to count number of the fields in the table, before embarking on any SQL. At the end of the look you would delete the link by calling DoCmd.DeleteObject.
It certainly looks neater than what you are trying to do.

Update model (edmx) with system tables (SYS.xxx) - Sybase

When selecting 'update model from database' none of the system tables (SYS. schema) is available from the list of tables.
How may I add a system table to my EF model.
Sybase (ASA12) is the database platform which I am using.
As a workaround I created a view on the system table.
It is then available and may be updated automated by the edmx generator
I created a script that recreates all the catalog views, i.e. sys.*, as views in a user schema:
Note: This is T-SQL, and SQL Server object names, but I'm sure you can adapt the concept to Sybase.
SELECT
'CREATE VIEW ' + 'dpc.' + name + ' AS SELECT * FROM ' + 'sys.' + name + char(13) + char(10) + ' GO' + char(13) + char(10)
FROM
sys.all_objects
WHERE
type = 'v'
and is_ms_shipped = 1
and schema_name(schema_id) = 'sys'
ORDER BY
name
Then I ran the script output by the above query, which copied each sys.x view to a new dpc.x view, and added all the dpc.* views to my EDMX model.