A good article on SQL Transactions

by John 11. May 2010 08:28
Tags:
Categories: SQL

Open DBDiff - a viable alternative?

by John 24. March 2010 02:46

Get it here.
Tags:
Categories: SQL

Msg 1934, Sev 16: UPDATE STATISTICS failed because the following SET options have incorrect settings: 'ARITHABORT'.

by John 17. March 2010 06:34

Got this when I ran my sp__updatestats procedure in a SQL Agent jobstep, but it worked in Query Analyzer. The reason? The default settings are probably different in QA rather than external connections similar to SQL Agent.

Msg 1934, Sev 16: UPDATE STATISTICS failed because the following SET options have incorrect settings: 'ARITHABORT'. [SQLSTATE 42000]

The solution was to recreate the procedure that ran UPDATE STATISTICS using a SET command within the procedure. 

CREATE PROCEDURE dbo.sp__UpdateStats
AS
SET 
ARITHABORT ON 
SET 
QUOTED_IDENTIFIER ON 
...

We started to get this problem after I added a computed column to a table. 

Tags:
Categories: ERRORs | SQL | SQLAgent

Getting started in SSIS dynamic packages

by John 4. March 2010 06:23

Finally getting around to replacing our DTS ActiveX scripts that make DTS Packages dynamic. Passing in variables on command line, allowed us to change our source, destination, remap transformations etc.

Getting started in SSIS.

1) Start with this list of Best Practices.
2) Follow this link to a great tutorial about using SSIS control flow script task.

 

Tags: ,
Categories: SQL | SSIS

Use COALESCE to put a column of data into a delimited list variable

by John 31. December 2009 01:41

Here's an example of how to create a delimited list, using a field of rows of data and COALESCE.

CREATE TABLE #tmp (ID INT IDENTITYValue VARCHAR(10))

INSERT #tmp (ValueVALUES(@@IDENTITY)
INSERT #tmp (ValueVALUES(@@IDENTITY)
INSERT #tmp (ValueVALUES(@@IDENTITY)
INSERT #tmp (ValueVALUES(@@IDENTITY)
INSERT #tmp (ValueVALUES(@@IDENTITY)
INSERT #tmp (ValueVALUES(@@IDENTITY)
INSERT #tmp (ValueVALUES(@@IDENTITY)
INSERT #tmp (ValueVALUES(@@IDENTITY)
INSERT #tmp (ValueVALUES(@@IDENTITY)
INSERT #tmp (ValueVALUES(@@IDENTITY)

DECLARE @vcList VARCHAR(100)
SELECT @vcList COALESCE(@vcList ';''')
ISNULL(Value'0')
FROM #tmp
-- ORDER BY might not work
PRINT @vcList

Resulting in:
10;1;2;3;4;5;6;7;8;9

NOTE:
Don't be surprised when ORDER BY doesn't work.  

'SQL Server query processor builds an different execution plan when expressions are applied to columns in the query's ORDER BY clause, than when those same expressions are applied to columns in the query's SELECT list. The decision made by the query processor is based on the cost of possible execution plans.'

Tags:
Categories: SQL

Can we use the SQL 2005 engine for queries against a SQL 2000 linked server?

by John 28. December 2009 04:40

You bet!

  1. On your SQL 2005/8 instance create a linked SQL 2000 server
  2. Run your SQL 2005 query using something like PIVOT or UNPIVOT against the SQL 2000 linked server table.
SELECT  [ND],
        
[SD],
        
[TN],
        
[OR],
        
[VA]
FROM    (
         
SELECT State,
                
SalesAmt
         
FROM   sqltest.northwind.dbo.Sales
        
p PIVOT SUM(SalesAmtFOR State IN ([ND][SD][TN][OR][VA]) ) AS pvt
 
Here's a link to an article example about PIVOT, UNPIVOT.
 
Tags:
Categories: SQL

[ODBC Driver Manager] Driver does not support this function ... 'MSDASQL' IDBInitialize::Initialize

by John 2. September 2009 07:17
On a new SQL Server I was trying to run an OPENROWSET query that opened a Foxpro free table and was getting this error. It turned out the latest Foxpro drivers were not yet installed. An easy way to find the drivers and their dates is to open the ODBC Data Source Administrator and click on drivers. You could also test by trying to create an ODBC connection to Foxpro.
 
Server: Msg 7399, Level 16, State 1, Line 1
OLE DB provider 'MSDASQL' reported an error.  
[OLE/DB provider returned message: [Microsoft][ODBC Driver Manager] Driver does not support this function]
OLE DB error trace [OLE/DB Provider 'MSDASQL' IDBInitialize::Initialize returned 0x80004005:   ].
 
e.g. 

SELECT *   
FROM  OPENROWSET (
  
'MSDASQL','Driver=Microsoft Visual FoxPro Driver;SourceDB=E:\DATA;SourceType=DBF;',
  
'SELECT  * FROM MyFreeTable;'
)

Once the correct drivers were installed we could run the query, above, and create ODBC connections. Then, we were getting this error, but only when not running directly on the server itself.

OLE DB provider "MSDASQL" for linked server "(null)" returned message "[Microsoft][ODBC Visual FoxPro Driver]File 'hprofile.dbf' does not exist.".
Msg 7350, Level 16, State 2, Line 1
Cannot get the column information from OLE DB provider "MSDASQL" for linked server "(null)".

So, our OPENROWSET queries and the linked server queries could be run locally to the server. There must still be some proxy permissions or security something we still need to set. 

Still, another error we're getting is this: 
Cannot get the schema rowset 'DBSCHEMA_CATALOGS' for OLE DB provider "SQL Server" for linked server "(null)" -- huh?

Tags:
Categories: SQL | SQL Server

Import EXCEL data using OPENDATASOURCE

by John 3. July 2009 07:25

Don't forget that now that it is in the tempdb table you can script out a field descriptor or a CREATE table statement. Use that for a permanent import table elsewhere.

USE tempdb
GO
SELECT *
INTO MyEXCELSheet
FROM OPENDATASOURCE(
'Microsoft.Jet.OLEDB.4.0',
'Data Source="E:\atest.xls";Extended properties=Excel 5.0'
)...Sheet1$
--
-- Now use Query Analyzer to script out the table in tempdb. Here's a sample script.
--
CREATE TABLE [dbo].[MyEXCELSheet] (
[CustomerID] [nvarchar] (255) NULL ,
[EmployeeID] [float] NULL ,
[Freight] [float] NULL ,
[OrderDate] [datetime] NULL ,
[OrderID] [float] NULL ,
[RequiredDate] [datetime] NULL ,
[ShipAddress] [nvarchar] (255) NULL ,
[ShipCity] [nvarchar] (255) NULL ,
[ShipCountry] [nvarchar] (255) NULL ,
[ShipName] [nvarchar] (255) NULL ,
[ShippedDate] [datetime] NULL ,
[ShipPostalCode] [float] NULL ,
[ShipRegion] [nvarchar] (255) NULL ,
[ShipVia] [float] NULL
) ON [PRIMARY]
END
GO
Tags:
Categories: EXCEL | SQL

Linked Server - commands, one hop

by John 3. July 2009 07:17

Easy linked server setup for a one hop query.

PC (A) to SERVER (B) will work.
PC (A) client tools to SERVER (B) and four-part queries from SERVER (B) connection to SERVER (C) will not work without some kind of delegation.

See "How to set up a Kerberos Authentication Scenario with SQL Server Linked Servers" and this "Allow a computer to be trusted for delegation for specific services"


-- Add a linked server
EXEC sp_addlinkedserver 
@server = N'SQLDEV',
@srvproduct  = N'SQL Server'
GO
-- And some options
exec sp_serveroption 
@server = N'SQLDEV', 
@optname = N'data access', 
@optvalue = N'true' 
exec sp_serveroption N'SQLDEV', N'collation compatible', N'true' 
GO
EXEC sp_addlinkedsrvlogin 
@rmtsrvname = 'SQLDEV'
,@useself = 'TRUE'
--     ,@locallogin = 'sa'    -- this doesn't seem to be required.
GO
EXEC sp_helpserver 'SQLDEV'
EXEC sp_helplinkedsrvlogin 'SQLDEV'
GO
-- This query will work from one hop. e.g. If you are querying directly from one local server to another.
-- You can't do this by Query Analyser on PC A, logging into server B, and running the 4 part query agains
-- Server C.
select * from sqldev.master.dbo.sysservers
GO
-- Some options
-- exec sp_serveroption N'SQLDEV', N'query timeout', 0 
-- exec sp_serveroption N'SQLDEV', N'collation name', N'null' 
-- exec sp_serveroption N'SQLDEV', N'use remote collation', N'true' 
-- exec sp_serveroption N'SQLDEV', N'rpc', N'true' 
-- exec sp_serveroption N'SQLDEV', N'rpc out', N'true' 
-- exec sp_serveroption N'SQLDEV', N'data access', N'true' 
-- exec sp_serveroption N'SQLDEV', N'collation compatible', N'true' 
-- exec sp_helplinkedsrvlogin N'SQLDEV'
-- exec sp_addlinkedserver N'SQLDEV' 
-- Clean up our test
--  Exec Sp_dropserver 'SQLDEV', droplogins
 
Tags:
Categories: SQL

[Microsoft][SQL Native Client]Unable to open BCP host data-file

by John 3. July 2009 07:10
Sometimes this ERROR is just file system permissions. It is definatelya generic error and not reporting what is actually going on.

Test by trying to write the the directory e.g. from SQL
EXEC xp_cmdshell '@ECHO hi! > E:\MSSQL\Tests\hi.txt'

If you get a security message, change the permissions of the directory e.g. Tests, above, to RW.
Categories: DOS | SQL