In this part I’ll help you in creating a Linked Server with Excel. Or simply how to import/query Excel in Sql Server.
In Sql Server 2005 you need to enable the 'Ad Hoc Distributed Queries'. For that first execute the following scripts.
sp_configure 'show advanced options', 1
GO
RECONFIGURE WITH OverRide
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE WITH OverRide
GO
And now here go
1.
SELECT * FROM OPENDATASOURCE ('Microsoft.Jet.OLEDB.4.0', 'Data Source=E:\Mangal.xls;Extended Properties=Excel 8.0')...Sheet1$
2.
SELECT * FROM OPENROWSET ( 'Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=E:\Mangal.xls', Sheet1$)
3.
SELECT * FROM OPENROWSET ( 'Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=E:\Mangal.xls', 'SELECT * FROM [Sheet1$]')
Here Excel 8.0 means this is applicable to Excel 2002, Excel 2000, or Excel 97 workbook.
Database means path of your excel book and its name.
Now you can do any Sql query with excel that you usualy do with any table. For example you can join excel with other tables, you can insert excel rows in other tables.
Lets say you want to insert first 3 columns of Excel sheet into table Customers.
You can do
INSERT INTO customers (CustId, CustomerName, Address )
SELECT Custid, CustomerName, Address
FROM OPENROWSET ( 'Microsoft.Jet.OLEDB.4.0', 'Excel 8.0; Database=E:\Mangal.xls', 'SELECT * FROM [Sheet1$]')
Or to join a table with Excel
SELECT A.ColumnName, E.ColumnName
FROM OPENROWSET ( 'Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=E:\Mangal.xls', 'SELECT * FROM [Sheet1$]' ) E
INNER JOIN Sql_Table A
ON a.id=e.id
- Mangal Pardeshi.
Wednesday, November 26, 2008
Sunday, November 23, 2008
Some Useful Views And SPs in Sql Server.
Here is the good collection of views and Store Procedure which can be very useful.
Applicable for Sql Server 2005 and +
VIEWS : To use all the views you just need to do SELECT * FROM VIEW_NAME
e.g SELECT * FROM sys.databases
-- I'll Add some more very soon.
- Mangal Pardeshi.
Applicable for Sql Server 2005 and +
VIEWS : To use all the views you just need to do SELECT * FROM VIEW_NAME
e.g SELECT * FROM sys.databases
- sys.databases : Lists all the databases in Sql Server.
- sys.tables : Lists all the Tables in the database.
- sys.views : Lists all the views in the database.
- sys.procedures : Lists all the Procedures in the database.
- sys.triggers : Lists all the Triggers in the database.
- sys.columns : Lists all the columns of tables in database.
- sys.syscolumns : Lists all the columns in database including of those SP.
- sys.key_constraints : Lists all primary key or unique constraints in database. For primary key TYPE = 'PK' and for unique keys TYPE = 'UQ'
- sys.check_constraints : Lists all the Check Constraints in the database.
- sys.default_constraints : Lists all the Default Constarints in the database.
- sys.foreign_keys : Lists all the Foreign Keys in the database.
- sys.syslogins : Lists all the login names in server.
- sys.sql_logins : Lists all the Sql Authentication Logins in server.
- sys.sysusers : Lists all the users in database.
-- I'll Add some more very soon.
- Mangal Pardeshi.
Saturday, November 22, 2008
Sql Server Connection Issues - Msg 18456
Now I'll try to help you understand the connection issues with Sql Server.
Today we will focus on the error
"Msg 18456, Level 14, State 1, Server, Line 1"
"Login failed for user ''."
This error is nothing but an authentication failure that involves a bad password or user name. So whenever you get a similar error be sure that is somthing related to user name or password.
And key to solve the issue is STATE number you get in the error.
Today we will focus on the error
"Msg 18456, Level 14, State 1, Server
"Login failed for user '
This error is nothing but an authentication failure that involves a bad password or user name. So whenever you get a similar error be sure that is somthing related to user name or password.
And key to solve the issue is STATE number you get in the error.
- 2 and 5 : Invalid userid.
- 6 : Attempt to use a Windows login name with SQL Authentication.
- 7 : Login disabled and password mismatch.
- 8 : Password mismatch.
- 9 : Invalid password.
- 11 and 12 : Valid login but server access failure.
- 13 : SQL Server service paused.
- 16 : User don't have permission to log into Server.
- 18 : Change password required.
And as it seems you get the error Msg 18456 when you trying to connect Sql server in Sql Authentication.
Some points I like to add....
State 23 : Is one of the very rare errors, normaly happens when you try to connect to server when it is shutting down.
States 11 and 12 : When the Windows user trying to access the server and he doesn't have rights. Chances are you are on Windows Vista :).
- Mangal Pardeshi.
Date Formats In SQL SERVER.
In this post I will try to explain how to convert date in required format.
As we know by default (U.S. English) Sql Server shows dates in mdy.
i.e.
2008-11-22 14:02:12.513
OR
11/22/2008 2:02:00 PM
And so many times you wants the date in dd/mm/yy or dd Mon yyyy or any other format you want.
The function used by sql server to convert the dates in required format is CONVERT. Which I personaly find more easy to use compare to to_date used by Oracle or other fuctions used by other DBMS.
Syntax for the CONVERT is
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
Where data_type can be either string datatype (char/ varchar) or datetime.
And Style in nothing but numbers in 1st 2 columns in following table .
1. Now for example If I want the date in "mon dd yyyy"
I will choose the style as 100 (from the above table)
SELECT CONVERT( varchar(50), date_column, 100)
FROM table_name
go;
e.g.
SELECT CONVERT(varchar(50), GETDATE(), 100)
GO
/* output
Nov 22 2008 2:21PM
*/
2. Or if I want mm/dd/yyyy I will select style 101
SELECT CONVERT(varchar(50), GETDATE(), 101)
GO
/* output
11/22/2008
*/
Now if instead of 2008 if I want only 08 I will select style from 1st column instead of column 2 which is style 1
SELECT CONVERT(varchar(50), GETDATE(), 1)
GO
/* output
11/22/08
*/
So if you want the century part in year (2008, 2009)select style from column 2 (100, 101, 102) or if you want only 2 digits of year (08, 09) select style from 1st column (1, 2, 3).
For more on CONVERT see
CAST and CONVERT (Transact-SQL) Books Online
- Mangal Pardeshi.
As we know by default (U.S. English) Sql Server shows dates in mdy.
i.e.
2008-11-22 14:02:12.513
OR
11/22/2008 2:02:00 PM
And so many times you wants the date in dd/mm/yy or dd Mon yyyy or any other format you want.
The function used by sql server to convert the dates in required format is CONVERT. Which I personaly find more easy to use compare to to_date used by Oracle or other fuctions used by other DBMS.
Syntax for the CONVERT is
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
Where data_type can be either string datatype (char/ varchar) or datetime.
And Style in nothing but numbers in 1st 2 columns in following table .
1. Now for example If I want the date in "mon dd yyyy"
I will choose the style as 100 (from the above table)
SELECT CONVERT( varchar(50), date_column, 100)
FROM table_name
go;
e.g.
SELECT CONVERT(varchar(50), GETDATE(), 100)
GO
/* output
Nov 22 2008 2:21PM
*/
2. Or if I want mm/dd/yyyy I will select style 101
SELECT CONVERT(varchar(50), GETDATE(), 101)
GO
/* output
11/22/2008
*/
Now if instead of 2008 if I want only 08 I will select style from 1st column instead of column 2 which is style 1
SELECT CONVERT(varchar(50), GETDATE(), 1)
GO
/* output
11/22/08
*/
So if you want the century part in year (2008, 2009)select style from column 2 (100, 101, 102) or if you want only 2 digits of year (08, 09) select style from 1st column (1, 2, 3).
For more on CONVERT see
CAST and CONVERT (Transact-SQL) Books Online
- Mangal Pardeshi.
Subscribe to:
Posts (Atom)