Let’s say you’re writing T-SQL code and need to make sure that your query returns one and only row. If no records returned – an error message needs to be shown. If more than one record is returned – another error message needs to be show. The code goes something like this:
-- ... query runs here and returns row
IF @@ROWCOUNT = 0 RAISERROR('No records found', 18, 1)
ELSE IF @@ROWCOUNT > 0 RAISERROR('More than one record found', 18, 1)
-- ... continue when exactly one row is returned
Now if your query returns 3 records you’d expect it to trip the second IF statement and raise error with correct message. Unfortunately this doesn’t happen. The reason being – the first IF statement resets @@ROWCOUNT to 0. So, to avoid this we need to preserve the @@ROWCOUNT value in a local variable:
DECLARE @iRowCount int
-- ... query runs here and returns row
SET @iRowCount = @@ROWCOUNT
IF @iRowCount = 0 RAISERROR('No records found', 18, 1)
ELSE IF @iRowCount > 0 RAISERROR('More than one record found', 18, 1)
-- ... continue when exactly one row is returned
This way count of rows returned by the query is saved and is not affected by any following statements.
Let’s say you have a table with some dates and numeric values e.g.:
2011-08-19 16
2011-08-22 45
2011-08-24 62
2011-08-25 88
2011-08-27 17
2011-08-28 35
2011-09-01 10
2011-09-02 79
2011-09-03 70
2011-09-07 83
As you can see not all dates are in the sequential order, 24th comes after 22nd etc. But in many cases you need this data to be sequential, for example if this data feeds a chart you need to fill it with missing dates and 0 for a value. There is an easy way to achieve this. Continue reading 'TSQL: Filling missing date range'»
This was driving me nuts. I have a very basic SQL code similar to
ALTER PROCEDURE Proc1(@val1 NVARCHAR(max))
AS
BEGIN
-- some code
EXEC Proc2 @val2 = @val1
-- some other code
END
I was getting error “String or binary data would be truncated”, but only when 2 conditions were met:
- @val1 is quite large
- Either SQL Server/computer just restarted or stored procedure Proc2 was just updated (ALTER)
Error happened just by the fact of the EXEC Proc2 being there it didn’t even had to do anything, it could RETURN straight away. Both @val2 an @val1 are of a type NVARCHAR(max) so there is no reason for the error. The error happened only once, after that I can pass data of any size – and no error would happen. Like I said – nuts. Continue reading 'Elusive “String or binary data would be truncated” error'»
This has probably been discussed a lot before, but just in case here it is again, possible solution for following scenario:
You’re calling SQL Server stored procedure from your .NET code and it runs extremely slow. When you run same SP with exactly the same parameters (as captured by SQL Server Profiler) directly in SQL Server Management Studio, it runs very fast. What gives?
Chances are – that SP was executed before and query plan was cached for the specific parameters. To avoid this add WITH RECOMPILE option to your CREATE PROCEDURE or ALTER PROCEDURE statement. This will force SQL Server to create a new query plan every time SP runs, perhaps adding slight overhead, but creating an optimized path that will cover that overhead tenfold.
While connecting from an ASP.NET application to an Oracle database via OLEDB I got following error:
OraOLEDB.Oracle Provider is not registered on the local machine
Now I know that the driver was installed and registered. I downloaded an official driver from Oracle WebSite. In my case it was ODAC112021Xcopy_x64.zip 64-bit version for XCopy deployment. It installs in 2 easy steps:
- Unzip downloaded file into any folder
- Run (as administrator) command: INSTALL TYPE PATH NAME DEPENDANCIES
Where
- TYPE – type of installation (e.g. OLEDB, basic etc.)
- Path – where you want driver installed
- Name – Oracle home name
- Dependencies – true/false whether to install dependencies (e.g. instant client)
So my command was
INSTALL ALL “C:\Program Files\Oracle64Driver” Oracle64Driver TRUE
Which copied the files and created correct Registry entries (I checked). And still I was getting the error. I Googled it (a lot) but majority of suggestions was that the error is due to Windows ACL and correct permissions should be set on the driver folder. Didn’t help me.
So I fired up trusted ProcessMonitor and it showed that W3WP.EXE (ASP.NET process) was trying to access missing OCI.DLL file in the path C:\Program Files\Oracle64Driver\Bin, e.g. in the Bin folder of the path were the driver was installed. Looking back at the place were I unzipped the original driver files I found that DLL inside of “instantclient” folder. So I copied entire content of that folder into Bin folder at the destination. And Voila! The error disappeared.
Apparently Instant Client files aren’t copied by the installer even when Dependencies option is set to true.
Linq2Sql has a great use of stored procedures – it converts them into methods which you can easily call using standardized .NET syntax. For example if you have SP:
ALTER PROCEDURE MyProcedure(MyParam int) ...
after dragging it into Linq2Sql designer you can call it in your .NET code like this:
Dim aResults = MyDbContext.MyProcedure(2011)
but there are 2 caveats. Continue reading 'Stored Procedure in LINQ2SQL query'»
Imagine following scenario: an SSRS report has a dropdown list “lookup” parameter based on a stored procedure. When report runs, user selects a value from the dropdown, clicks “View Report” and report is generated. The challenge is – the “lookup” parameter (and underlying stored procedure) needed to be filtered by another “filter” parameter – and this one is not available in SSRS interface, but instead is passed from ReportViewer control from an ASP.NET application.
In order to successfully implement this use case 2 items need to be addressed:
First in the report itself parameters need to be ordered in such way so “filter” comes before “dropdown”. Parameters can easily be arranged in Business Intelligent Development Studio, by expanding Parameters node, selecting a parameter, and using arrow buttons in Report Data menu.
Second in the ASP.NET application, configuring ReportViewer control (setting credentials, server URL, report path and our “filter” parameter) needs to be done in Page_Init event in the “If Not IsPostback” block.
UNION is a very common operator used in SQL, it allows to combine result of multiple queries into one. Unfortunately if you want to sort this unified resultset sorting is applied to entire result at once. But what if you wan to sort queries-participants individually?
Let’s say you have 2 following queries from Northwind database:
SELECT TOP (5) CompanyName FROM Suppliers ORDER BY CompanyName
Aux joyeux ecclésiastiques
Bigfoot Breweries
Cooperativa de Quesos 'Las Cabras'
Escargots Nouveaux
Exotic Liquids
and
SELECT TOP (5) ProductName FROM Products ORDER BY ProductName
Alice Mutton
Aniseed Syrup
Boston Crab Meat
Camembert Pierrot
Carnarvon Tigers
And now you want to combine them into a single result, adding title lines to separate each result. The direct approach would be:
SELECT 'Suppliers' AS Name
UNION
SELECT TOP (5) CompanyName FROM Suppliers
UNION
SELECT 'Products'
UNION
SELECT TOP (5) ProductName FROM Products
ORDER BY Name
But result is far from what we wanted
Alice Mutton
Aniseed Syrup
Aux joyeux ecclésiastiques
Bigfoot Breweries
Boston Crab Meat
Camembert Pierrot
Carnarvon Tigers
Cooperativa de Quesos 'Las Cabras'
Escargots Nouveaux
Exotic Liquids
Products
Suppliers
Entire resultset is sorted uniformally and products are mixed with suppliers.
Continue reading 'Individual Sorting of SELECT queries in the UNION'»
It’s a common scenario, your table has several records with identical values and you need to leave only one, deleting the rest. Here is a generic TSQL query (SQL Server 2005 and above) that does just that in a few lines:
WITH DUP_TABLE AS
(SELECT ROW_NUMBER()
OVER (PARTITION BY FIELD1, FIELD2 ORDER BY FIELD1, FIELD2) As ROW_NO
FROM ORIGINAL_TABLE)
DELETE FROM DUP_TABLE WHERE ROW_NO > 1
Here ORIGINAL_TABLE is your table with duplicates. FIELD1 and FIELD2 are columns with duplicates value (feel free to add or remove columns to suit your needs). Internal query assigns a row number to each duplicate record and DELETE statements that uses that CTE deletes all the rows except the one with Row Number = 1
Consider following scenario: While developing for SQL Server Reporting Services you created a report in Business Intelligent Studio (or generated RDL file elsewhere and then added it to Studio’s project). Everything works fine, you’re able to preview report. And then you need to change Data Source (one example: you created a shared Data Source and now want your report to use it). After you do that any attempt to run the report results in error: Query execution failed … Expected parameter ….
The reason in this case – when you change the data source thru GUI – it resets query used by the report. It can change query type from StoredProcedure to Text and also remove all default query parameters (hence error above).
There’re 2 possible solutions here: First – instead of using GUI, manually change Data Source in the XML source of the report file (in Solution Explorer right mouse click on the report file and select “View Code”). You will have to change it in several places, so be careful. Second – re-create query with its default parameters after changing Data Source in GUI.