How can I do a dynamic pivot in SQL Server 2000 - sql

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.

Related

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

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

Dynamic SQL Query for Matrix

I need some help in creating a stored procedure or a dynamic SQL query to query a matrix and have a output in a required format.
Database: SQL Server 2014
States table - screenshot:
Required output
More states will added to the table. So the Script should be able to dynamically get all the rows and columns.
The output is to create a new table.
Thanks in advance.
Try this below links for your reference it may help you for logic
How to pivot table in SQL Server with a stored procedure?
Dynamic Pivot Queries with dynamic dates as column header in SQL Server
Note: The above links for pivoting.better refer it and do the unipovt

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.

Informatica - How to Pass queries from a table to SQL Transformation and get the results

SO heres what I am trying to do.
I have a table which has 2 columns - QC_Check and Query. For each QC_Check I have a query. There are several records like this.
Is there a way using SQL transformation that, I can fetch the SQL query stored in the Query column to Informatica, run the queries in Teradata and get the results stored somewhere.
Although, I have not tried it myself, this should be possible using SQL transformation in Query mode with Dynamic SQL Queries.
Use the table with Query column as a source. Create a SQL transformation with Query mode. Connect the Query column to the SQL transformation.
Write ~Query_Port~ in the SQL editor in the SQL transformation:
If you want to capture the results from your query, you have to configure output ports for columns you retrieve from the database.