Select multiple columns as JSON in a single query [duplicate] - sql

I am using SQL Server 2008 and want to convert table data into json format. Can I convert it directly through firing query?

I have created a stored procedure that can take your table and output JSON. You can find it at
GitHub - SQLTableOrViewToJSON
Once you run the stored procedure, you can output your table by making a call like the following:
EXEC SQLTableOrViewToJSON 'MyTable', 'C:\WhereIStowMyJSON\'

Built in support for formatting query results is added in SQL Server 2016 and it will be available in Azure Database.
In older versions you would need to use CLR or some heavy TSQL like:
Producing JSON Documents from SQL Server queries via TSQL

The above solutions seem unnecessarily complicated; starting with 2008, SQL Server supports the following syntax (answer from DBA StackExchange)
SELECT * FROM dbo.x FOR JSON AUTO;
The above query returns a single JSON-formatted column that looks like this (assuming dbo.x contains columns col1~col4of corresponding types):
[{"col1":"val1","col2":"val2","col3":5,"col4":"2019-02-11"}]
More details/options here: https://learn.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server

Related

How can we write a parameterized query in ssrs report when we use Snowflake as data source

I'm working on Sql server to Snowflake migration project,So i pointed ssrs reports to Snowflake data source and converting sql queries as per snowflake,but i'm not able to get how can we write queries for parameterized reports.Example select * from Student where Std_id=#id,want to convert to snowflake query.
You can use SQL variables:
https://docs.snowflake.com/en/sql-reference/session-variables.html
or Snowflake Scripting Variables:
https://docs.snowflake.com/en/developer-guide/snowflake-scripting/variables.html
I think the SQL variables would be helpful in your case, but Snowflake Scripting variables would be more similar to your SQL Server # variables.

Migrate table from Oracle to SQL Server

Migrate a table from Oracle to SQL Server.
I have used Toad to export (select * from table) into a pipe delimited .txt file so it can be used to be consumed in SQL Server. Now the Oracle table has a DATE column and the output from Toad for that column is (2/26/2016 3.05.10.000000 PM). This format is not being compatible for the datetime column in SQL Server side.
I feel we can convert the date in Oracle to a compatible SQL Server format for easier ingestion.
Please help me understand the conversion both from Oracle to a compatible SQL Server format.
Create Oracle Linked server in SQL Server with ODBC connection. and use that Linked server to play with Oracle and SQL Server tables using SQL Server.
You must understand that DATE datatypes are binary data. Using to_date() on a column that is already a DATE is inappropriate. It forces oracle to perform (behind the scenes) a to_char() on the DATE column in order to produce character data that is the required input to to_date(). Then, when you see (in your text csv file) that it has produced a "date" in some particular format, it is because oracle has then had to run the result of your to_date() back through to_char(), using the default NLS_DATE_FORMAT setting to produce a character string for the text output.
So your solution is this:
First, determine what text format of a date MSSQL wants when it uses this csv file. I don't know what that is, but for the sake of argument, let's say it is 'yyyy-mm-dd'. With that information, construct your SELECT in oracle like this:
select mycol1,
to_char(my_date_col,'yyyy-mm-dd'),
mycol2
from my_table;
That said, I agree with the others, why bother with this cumbersome process in the first place? Or even some other intermediary like SSIS? Why not just create a shared server in MSSQL and query the oracle table directly? Or create a database link in the Oracle DB and, using the oracle transparent gateway as the conduit, INSERT directly into the MSSQL table from Oracle? Either the linked server or the database link will be much faster than any external process.
I would suggest a best way to transfer Oracle table to SQL Serveris by using SSIS package.
You can have a Source as Oracle and your conversion issue can be fixed by Data
Conversion task and your Destination can be SQL Server.

How to convert data to json format in SQL Server 2008?

I am using SQL Server 2008 and want to convert table data into json format. Can I convert it directly through firing query?
I have created a stored procedure that can take your table and output JSON. You can find it at
GitHub - SQLTableOrViewToJSON
Once you run the stored procedure, you can output your table by making a call like the following:
EXEC SQLTableOrViewToJSON 'MyTable', 'C:\WhereIStowMyJSON\'
Built in support for formatting query results is added in SQL Server 2016 and it will be available in Azure Database.
In older versions you would need to use CLR or some heavy TSQL like:
Producing JSON Documents from SQL Server queries via TSQL
The above solutions seem unnecessarily complicated; starting with 2008, SQL Server supports the following syntax (answer from DBA StackExchange)
SELECT * FROM dbo.x FOR JSON AUTO;
The above query returns a single JSON-formatted column that looks like this (assuming dbo.x contains columns col1~col4of corresponding types):
[{"col1":"val1","col2":"val2","col3":5,"col4":"2019-02-11"}]
More details/options here: https://learn.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server

Single SQL query run against multiple linked SQL servers/DBs

I have a single SQL query that I need to run against ~25 different databases- each residing on a separate SQL server on the network. The query will run from a single central SQL server management studio, and the 24 other SQL server instances are linked. I have the query I need, and I tested that it works- however the goal is to create a script that queries each of the 25 separate SQL instances.
Instead of writing the query out 25 separate times within the script, I'm wondering if there's a way to utilize the single block of code to query each of the linked instances using an array, variables, DO/WHILE, a function or any other method.
Here's the query:
SET NOCOUNT ON
PRINT 'local server';
SELECT isc.ini_schema_name[Device], count(*) [Count]
FROM pharos.dbo.edi_pharos_stations eps, pharos.dbo.ini_schemas isc
WHERE eps.ini_schema_id = isc.ini_schema_id
GROUP BY isc.ini_schema_id, isc.ini_schema_name
For the purpose of this example, if I were to utilize the less-graceful approach of writing out the block of code 24 more times, this would be the next query in the script (to query SQL server hostnamed pharos90-2008).
PRINT 'Pharos90-2008';
SELECT isc.ini_schema_name[Device], count(*) [Count]
FROM [pharos90-2008].pharos.dbo.edi_pharos_stations eps, [pharos90-2008].pharos.dbo.ini_schemas isc
WHERE eps.ini_schema_id = isc.ini_schema_id
GROUP BY isc.ini_schema_id, isc.ini_schema_name
As you can see, the query / code is exactly the same except for the fact that it is referencing a separate linked SQL Server (query being run from a central SQL Server Management Studio).
The ultimate goal is to output the queried data for each SQL instance to a single .txt file; format being, print the name of each particular SQL server followed by the corresponding queried data.
Any advice as to how one would accomplish such a task?
Thanks in advance.
Well, one way would be to create a cursor to iterate all of your linked servers. (You can find linked servers like this...)
SELECT * FROM sys.servers WHERE is_linked = 1
Then, you could use the undocumented sp_MSForEachDB stored procedure to run a dynamic version of your query (changing the server on each iteration) on each database in the cursor's current server. If you search for sp_MSForEachDB you can find plenty of information. But here's one link to save time.

How can I do a dynamic pivot in SQL Server 2000

I want to write a SQL statement that uses a pivot in SQL Server 2000. The PIVOT keyword is not available in SQL Server 2000 so I found some examples that use a case statement but that requires that you know the column names beforehand which I won't. How do I do a pivot which dynamically generates the column names from the data it has available to it?
We create SQL commands with the CASE statements from our application and fire them at the database (any database, not specifically SQL server). First we determine the number of pivot columns and their names using one query, from those results we generate the next query.
So the first query to determine the columns looks somewhat like:
SELECT DISTINCT myField FROM myTable
Then we use all the values in this result to construct an SQL command where a CASE statement is generated for each value.
We wanted a databasebase agnostic solution so we do this processing outside the database but i'm sure you could do the same in a stored procedure withing SQL server itself.
I have not tried to replicate PIVOT on SQL Server 2000 but what I have done is use PIVOT when I do not know the column names beforehand. I had used ROW_NUMBER() to determine the column names instead. You can try that.