Whenever the column returned by the subquery contains a NULL value, NOT IN will always return an empty set. Filtering out the NULL values in the subquery will solve this problem.
select
*
from
table1
where
col1 not in(select col2 from table2 where col2 is not null)
Read SQL Server: JOIN vs IN vs EXISTS - the logical difference for a detailed explanation.
To keep track of favourite web sites, useful links, articles and own experiences which may be helpful in future software development.
Showing posts with label T-SQL. Show all posts
Showing posts with label T-SQL. Show all posts
Thursday, July 1, 2010
Monday, June 21, 2010
Corrupt index(es) in msdb
I was trying to delete a SQL Job using sp_delete_job. It continued to fail with the message:
(The command that fixed it is DBCC CHECKDB('msdb', REPAIR_REBUILD), but there is quite a bit of preparation that needs to be done before this command will execute successfully. See the blog post I referred to.)
Unable to find index entry in index ID 1, of table {object_id of table}, in database 'msdb'. The indicated index is corrupt or there is a problem with the current update plan. Run DBCC CHECKDB or DBCC CHECKTABLE. If the problem persists, contact product support.I found this blog post and the script in it repaired the msdb.
(The command that fixed it is DBCC CHECKDB('msdb', REPAIR_REBUILD), but there is quite a bit of preparation that needs to be done before this command will execute successfully. See the blog post I referred to.)
Tuesday, February 23, 2010
Don't be greedy in T-SQL queries
I am working on improving the performance of some stored procedures. The queries all make use of index seeks and index scans to get to the data, so, nothing I can improve there.
The procs call other procs which call some more procs. A join to a large table (+/-2million records) gets repeated in several of these procs.
So, I changed the deepest nested proc to return all the data which will be needed by the other procs. I removed the repeated join; and the performance problem is solved.
So, two very important tips for performance tuning queries:
The procs call other procs which call some more procs. A join to a large table (+/-2million records) gets repeated in several of these procs.
So, I changed the deepest nested proc to return all the data which will be needed by the other procs. I removed the repeated join; and the performance problem is solved.
So, two very important tips for performance tuning queries:
- Only return the records you are interested in; and
- Only touch a record once if possible.
Tuesday, February 16, 2010
CATCH block not executing after an error in the TRY block
Today we had trouble with the CATCH block in a T-SQL script which did not execute after an error in the TRY block. It turned out that there are times when your TRY block can fail and the CATCH block will get bypassed altogher.
Click here or on the title of this article to see the article by Alexander Kuznetsov about exactly this problem.
Our problem was a compile error: a column name we referenced in a join statement no longer existed.
Click here or on the title of this article to see the article by Alexander Kuznetsov about exactly this problem.
Our problem was a compile error: a column name we referenced in a join statement no longer existed.
Wednesday, December 3, 2008
Protocol error in TDS stream
Yesterday I struggled with the following issue in SSIS: I specified a stored proc as the SQL command text in an OLE DB Source in a data flow. When I clicked the Preview button the data I expected was returned. The Columns were also picked up successfully in the source editor.
But everytime I ran the package it failed with the following errors:
I am not going to repeat what he said in his post because he did a brilliant job of it. But in short: tables, views and table-valued UDFs have explicitly defined output columns; and stored procs don't. So SSIS will have guaranteed success in retrieving the correct output columns from tables, views and table-valued UDFs, while a stored proc can return different resultsets based on the input parameters.
As I said before, this does not fit my problem since SSIS did pick up the correct output columns, but I suspect that it missed some crutial metadata which wasn't available in the stored procedure.
But everytime I ran the package it failed with the following errors:
SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.After searching the whole day for a solution I finally stumbled accross this post by Adam Machanic. At first sight it did not fit my problem, but out of desperation I decided to give it a bash; and it worked.
An OLE DB record is available. Source: "Microsoft OLE DB Provider for SQL Server" Hresult: 0x80004005 Description: "Protocol error in TDS stream"...
SSIS Error Code DTS_E_PRIMEOUTPUTFAILED. The PrimeOutput method on component "OLE DB Source" (1135) returned error code 0xC0202009. The component returned a failure code when the pipeline engine called PrimeOutput(). The meaning of the failure code is defined by the component, but the error is fatal and the pipeline stopped executing. There may be error messages posted before this with more information about the failure...
A component has returned from its PrimeOutput call. : 1135 : OLE DB Source
SSIS Error Code DTS_E_THREADFAILED. Thread "SourceThread0" has exited with error code 0xC0047038. There may be error messages posted before this with more information on why the thread has exited...
SSIS Error Code DTS_E_THREADCANCELLED. Thread "WorkThread0" received a shutdown signal and is terminating. The user requested a shutdown, or an error in another thread is causing the pipeline to shutdown. There may be error messages posted before this with more information on why the thread was cancelled...
SSIS Error Code DTS_E_THREADFAILED. Thread "WorkThread0" has exited with error code 0xC0047039. There may be error messages posted before this with more information on why the thread has exited...
I am not going to repeat what he said in his post because he did a brilliant job of it. But in short: tables, views and table-valued UDFs have explicitly defined output columns; and stored procs don't. So SSIS will have guaranteed success in retrieving the correct output columns from tables, views and table-valued UDFs, while a stored proc can return different resultsets based on the input parameters.
As I said before, this does not fit my problem since SSIS did pick up the correct output columns, but I suspect that it missed some crutial metadata which wasn't available in the stored procedure.
Labels:
OLE DB Source,
SQL Server 2000,
SQL Server 2005,
SSIS,
T-SQL,
T-SQL Functions
Tuesday, July 15, 2008
Get all the dates in a range
Use this T-SQL script to get all the dates in a specified date range.
Please note that it will only work in SQL Server 2005 or later.
Please note that it will only work in SQL Server 2005 or later.
Monday, June 2, 2008
SET FMTONLY
When you want to retrieve the metadata for a table, view, stored procedure, etc without having any records returned, use SET FMTONLY ON. Only the column data will be returned.
Thursday, May 22, 2008
Get the SQL Server Instance name from T-SQL
To get the SQL Server instance name with T-SQL use one of the following two statements:
Click here to see more properties of the SERVERPROPERTY function.
- select @@SERVERNAME
- select SERVERPROPERTY('SERVERNAME')
Click here to see more properties of the SERVERPROPERTY function.
Thursday, May 15, 2008
Get the names of all tables used in a SQL Server view
I want to be able to quickly identify the tables which make up a SQL Server view. I know I can just open the script and look at it, but I have the following problems:
Take note: This script is by no means foolproof, and I haven't tested it very extensively. I am even ignoring the tables' schemas altogether. And if a view and a table would have the same name my script wouldn't know the difference.
But hey, it returns the information I want, so I am happy.
I didn't use the system view INFORMATION_SCHEMA.VIEWS because it truncates a view's T-SQL script in the VIEW_DEFINITION column when the script is too long.
I rather used syscomments because it spreads a view's script over several records when the script is too long for the text column. This enabled me to search the whole script for tables names.
I am not sure if syscomments will split a script in the middle of a word when the script is too long for the text column. If that would be the case I could miss some table names because they can be split.
(syscomments is a system view in the sys schema in SQL Server 2005. In SQL Server 7.0 and 2000 it is a system table in the dbo schema.)
- All the scripts are one liners for some strange reason.
- And some of the views contain large numbers of tables in the joins.
Take note: This script is by no means foolproof, and I haven't tested it very extensively. I am even ignoring the tables' schemas altogether. And if a view and a table would have the same name my script wouldn't know the difference.
But hey, it returns the information I want, so I am happy.
I didn't use the system view INFORMATION_SCHEMA.VIEWS because it truncates a view's T-SQL script in the VIEW_DEFINITION column when the script is too long.
I rather used syscomments because it spreads a view's script over several records when the script is too long for the text column. This enabled me to search the whole script for tables names.
I am not sure if syscomments will split a script in the middle of a word when the script is too long for the text column. If that would be the case I could miss some table names because they can be split.
(syscomments is a system view in the sys schema in SQL Server 2005. In SQL Server 7.0 and 2000 it is a system table in the dbo schema.)
Labels:
HowTo,
SQL Server,
SQL Server 2000,
SQL Server 2005,
T-SQL
Wednesday, May 14, 2008
Undocumented Stored Procedures
Apparently there are extended stored procedures which are not documented in the Books Online. In this post I want to create a list of those stored procs with links to articles about how to use them. So far I have only one proc, but I will update this post as soon as I come across more.
I don't think this one is an extended proc, but also undocumented:
- xp_ReadErrorLog (SQL Server 2005)
I don't think this one is an extended proc, but also undocumented:
EXEC sp_MSForEachTable 'EXEC sp_spaceused [?]' - It allows one to execute a T-SQL statement against each table in a database.
Friday, May 9, 2008
Best Practices for Troubleshooting Slow Running Queries
A colleague forwarded this article to me. Although the article contains very useful and valid tips, he pointed out that it does not discuss poorly written T-SQL as a possible cause for slow running queries.
Poorly written T-SQL is probably such a huge topic that it deserves an article of its own.
Anyhow, I hope you can use some of the troubleshooting tips.
Poorly written T-SQL is probably such a huge topic that it deserves an article of its own.
Anyhow, I hope you can use some of the troubleshooting tips.
Labels:
Best Practices,
Performance Tuning,
SQL Server,
T-SQL,
Troubleshooting
Wednesday, April 23, 2008
SQL Server 2005 ROW_NUMBER() function
Here's an example of how to use the row_number() function which became available in SQL Server 2005.
Click here for a detailed description of the syntax.
Click here for a Google search on this topic.
Run this script against the AdventureWorks sample database in SQL Server 2005. See how the row_number() function numbers the line items per sales order:
select
SalesOrderNumber,
LineItemNo = row_number() over(partition by SalesOrderNumber order by SalesOrderDetailID),
Product = p.Name,
OrderQty,
UnitPrice,
UnitPriceDiscount,
LineTotal
from
Sales.SalesOrderHeader h
inner join
Sales.SalesOrderDetail d
on
d.SalesOrderID = h.SalesOrderID
inner join
Production.Product p
on
p.ProductID = d.ProductID
Click here for a detailed description of the syntax.
Click here for a Google search on this topic.
Run this script against the AdventureWorks sample database in SQL Server 2005. See how the row_number() function numbers the line items per sales order:
select
SalesOrderNumber,
LineItemNo = row_number() over(partition by SalesOrderNumber order by SalesOrderDetailID),
Product = p.Name,
OrderQty,
UnitPrice,
UnitPriceDiscount,
LineTotal
from
Sales.SalesOrderHeader h
inner join
Sales.SalesOrderDetail d
on
d.SalesOrderID = h.SalesOrderID
inner join
Production.Product p
on
p.ProductID = d.ProductID
Monday, February 4, 2008
Change a T-SQL variable in an 'update' statement
declare @message varchar(1024)
set @message = 'New message'
update MyTable
set Column1 = 'New Value',
Column2 = @message,
@message = 'Message updated successfully'
where [Some condition]
select @message
If one or more records have been updated with the update statement, the @message variable will contain "Message updated successfully".
If no record was updated the variable will contain the original text "New message".
set @message = 'New message'
update MyTable
set Column1 = 'New Value',
Column2 = @message,
@message = 'Message updated successfully'
where [Some condition]
select @message
If one or more records have been updated with the update statement, the @message variable will contain "Message updated successfully".
If no record was updated the variable will contain the original text "New message".
Subscribe to:
Posts (Atom)
SQL Server
- 101 Things I wish you knew about SQL Server
- 102 SQL Server 2005 Tutorials and Articles
- Audit Trails and Logging Part 1 (sqlservercentral.com)
- Audit Trails and Logging Part 2 (sqlservercentral.com)
- Audit data using SQL Server 2005's COLUMNS_UPDATED function
- Calling a web service from a CLR proc
- Choosing between SQL Server Compact 3.5 and SQL Server 2005 Express Edition
- Deploying Database Developments
- Download SQL Server 2005 Express Edition
- Download SQL Server Compact 3.5
- How to return a result set from a SQL Server 2005 CLR stored procedure (mssqltips.com)
- How to return a result set from a SQL Server 2005 CLR stored procedure (mssqltips.com)
- Online Training (sqlservercentral.com)
- SQL Server 2008 Transparent Data Encryption getting started (MSSQLTips.com)
- SQL Server Audit in SQL Server 2008 – Part 1 (Database Journal)
- SQL Server Data Structure
- SQL Server Indexes: The Basics
- T-SQL Enhancement - APPLY Join Operator (drsql)
- T-SQL Ranking Functions (MSDN)
- T-SQL: Calendar tables
- T-SQL: Correlated Joins Using "Apply" (sqlservercentral.com)
- T-SQL: Identify Modified Columns in a Trigger (SQL Server Magazine)
- T-SQL: Identity Columns (simple-talk)
- T-SQL: More with SQL Server 2005 : Top n Per Group, Paging, and Common Table Expressions
- T-SQL: Obtaining Data as XML from SQL Server (MSDN)
- T-SQL: SQL Server 2005 Programmability Enhancements — Common Table Expressions (codeguru)
- T-SQL: SQL Server 2005: Creating User-defined Functions (Database Engine) (MSDN)
- T-SQL: SQL Server: JOIN vs IN vs EXISTS - the logical difference
- T-SQL: The "Numbers" or "Tally" Table: What it is and how it replaces a loop.
- T-SQL: The Curse and Blessings of Dynamic SQL
- T-SQL: The ultimate guide to the datetime datatypes
- T-SQL: Understanding SQL 2005's new PIVOT clause
- T-SQL: Use Dynamic SQL to Improve Query Performance
- T-SQL: Using the OUTPUT clause for practical SQL Server applications
- T-SQL: What's New in FOR XML in Microsoft SQL Server 2005 (MSDN)
- TechNet: Improving Performance with SQL Server 2005 Indexed Views
- The Truth about "Cursor Busting" in SQL
- Thinking Set-Based .... or not?
- Upgrading a Database SQL 2000 to SQL 2005 (sqlservercentral.com)
DTS and SSIS
- Download SQL Server 2005 DTS add-in
- Dynamically Generate Folders for File Output using Variables in SSIS (sqlservercentral.com)
- How to create an XML config file for a SSIS package
- SQL Server Integration Services (SSIS), ETLs and Best Practices (Brian's Blog)
- Transactions in SQL Server 2005 Integration Services