Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 conversion failed using OSQL but works in SSMS

Author  Topic 

ron_lhr
Starting Member

1 Post

Posted - 2010-06-28 : 12:17:15
Hi,

I've got a query that on my machine I run inside SSMS and it works nicely but if I fire up the command line and use osql and paste the query in or use sqlcmd and call it from a .sql file, it gives me this error:

Msg 245, Level 16, State 1, Server MSSQLCLUSTER\MSSQLCLUSTER, Line 1
Conversion failed when converting the varchar value 'Column 0' to data type int.

I've been googling like crazy on this and all I've managed to find from Microsoft's site is a big yellow box telling me that behaviors may be different between osql and SSMS ... but with no other guidance or information.

What do I need to do to get this to work in a manner that I can batch it? I don't want to run it manually by opening up SSMS every day.


The query:

SELECT
CAST("Column 0" as integer) as fsa_firm_ref,
CAST("Column 1" as varchar(400)) as firm_name,
CAST("Column 2" as int) as firm_legal_status_type_code,
CAST("Column 3" as int) as firm_type_code,
CAST("Column 4" as char(1)) AS auth_to_hold_client_money ,
CAST("Column 5" as varchar(255)) AS address_1 ,
CAST("Column 6" as varchar(255)) AS address_2 ,
CAST("Column 7" as varchar(255)) AS address_3,
CAST("Column 8" as varchar(255)) AS address_4 ,
CAST("Column 9" as varchar(255)) AS address_5 ,
CAST("Column 10" as varchar(255)) AS address_6 ,
CAST("Column 28" as varchar(15)) AS postcode ,
CAST("Column 29" as varchar(200)) AS telephone ,
CAST("Column 30" as varchar(200)) AS fax ,
CAST("Column 19" as varchar(30)) AS current_authorisation_status,
CAST("Column 20" as datetime) AS status_last_changed_date,
CAST("Column 21" as datetime) AS first_authorised_date ,
CAST("Column 22" as varchar(400)) AS sortkey ,
CAST("Column 23" as datetime) AS last_update_date,
getdate() as load_date
into firm_authorisation
from AUT
GO


tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-06-28 : 13:09:03
You need to put square brackets around your columns as what you've got is being interpreted as a data value and not a column name. Do it for all of your columns to avoid a similar error with the other ones. Or better yet, change your column names so that you don't have a space!

SELECT CAST ([Column 0] ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -