Showing posts with label syntax. Show all posts
Showing posts with label syntax. Show all posts

Thursday, March 29, 2012

DataType Conversion using WHERE IN ( )

I am getting a "Syntax error converting the varchar value '10,90' to a column of data type int." error when I run the following procedure:

@.myList varchar(200)

SELECT column1
FROM table1
WHERE table1.ID IN (@.myList)

When @.myList is a single value, I get no errors. However, when @.myList is a comma separated list like in the message above, I error out. I am using SQL Server 2000.

How else can I build this list of IDs? Thank you in advance for your comments.

--ColonelYou cannot do what you are trying to do. YOu need to use dynamic SQL, or send in a string and use a function to create a table variable and do the operation based upon a select on that table variable.|||I found that my varchar parameter was being sent in with single quotes around it. I removed these, and now my WHERE clause looks like this:

WHERE table1.ID IN (REPLACE(@.myList,'''',NULL))

and it works just fine.

I did not add those quotes to the list of values. I believe that SQL Server adds them to delimit the text. Thank you for your comments.

Wednesday, March 21, 2012

DataRow syntax

command.CommandText = "SELECT UserName from Users WHERE UserID = " = userID

Executing this command returns one table with one column with one row. What is the syntax for getting that value into a variable? I can get the information into a dataSet but I can't get it out. Should I be using a dataSet for this operation?

The rest of the code so far:

SqlDataAdapter dataAdapter =newSqlDataAdapter();

dataAdapter.SelectCommand = command;

dataAdapter.TableMappings.Add("Table","Users");

dataSet =newDataSet();

dataAdapter.Fill(dataSet);

Using that code, your data would be in dataSet.tables[0].rows[0][0].

If that's always just returning one value, you might want to look into using ExecuteScalar instead of the adapter and dataset.

|||

You can just use executeScalar method of SQl command below is example from VB.Net help for scalar modified a little:

Public Function AddProductCategory( _ ByVal UserID As Integer, ByVal connString As String) As Integer Dim Username As string = "" Dim sql As String = "SELECT UserName from Users WHERE UserID = @.USERID" Using conn As New SqlConnection(connString) Dim cmd As New SqlCommand(sql, conn) cmd.Parameters.Add("@.USERID", SqlDbType.Int) cmd.Parameters("@.USERID").Value = newName Try conn.Open() userName = Convert.ToInt32(cmd.ExecuteScalar()) Catch ex As Exception Console.WriteLine(ex.Message) End Try End Using Return newProdIDEnd Function
Thanks
|||

Got it working ... thank you for your help!

Monday, March 19, 2012

Datareader source/Data flow task property expression problems

Hi all,
I have the June CTP version of Yukon and it's various tools, and I'm having an issue with using property expressions or variable syntax within the SqlCommand string of a datareader source within a data flow task. It seems as if there were issues in past versions of doing this within data flow tasks, but I thought with the June CTP that this was a possibility. The documentation even states that you can do this within the sqlcommand property. Anybody else have this problem? Any solutions?
Thanks,
Adrian CrawfordHello Adrian,
Not sure exactly what you mean by "I'm having an issue with using property expressions or variable syntax" Are you referring to it error-ing out, or are the values not showing up properly?

Maybe the workaround in this post might help you?
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=70082

Jason|||Hey Jason,
Thanks for the reply. It is erroring out and not accepting my sqlcommand when I try to use any dts variables in the query. The datareader source works a little differently in that it doesn't give you the option to parse/build your query like other tasks do. Unfortunately there is not much of an error message to give. I get...
Further changes need to be made before the current settings can be saved to the component. Warnings reported by the component are:
And then nothing is listed. I'm trying to query an Oracle db and use dts vars in the query, but i have a feeling they are not getting parsed before being sent to Oracle.
Adrian
|||Hi Adrian,
Can you share your expression? I'd like to try to reproduce this so I can see why the error message is incorrect.
Also, what is the scope of the variables you are using?

Thanks
Mark|||Mark,
Thanks for your response. I figured out my problem in another recent post here:
http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=73466
Basically I found that you can't directly enter variables or expressions in the sqlcommand box, since it will not parse it before being sent. I found the round about way in the above post.
Thanks,
Adrian

Tuesday, February 14, 2012

database use syntax help

I have database called 'test1' and when i use the code below im getting this error
Server: Msg 170, Level 15, State 1, Line 9
Line 11: Incorrect syntax near '@.dbName'.

plz help, here is the code

DECLARE @.dbName varchar(50)
DECLARE @.index int

SET @.index = 1
SET @.dbName ='test'

set @.dbName = @.dbName + CAST(@.index as varchar)
use @.dbNameu cannot do that. if u want to select rows from table1 of test1 database
select * from test1..table1

or u can use dynamic sql like

DECLARE @.dbName varchar(50)
DECLARE @.index int

SET @.index = 1
SET @.dbName ='test'

set @.dbName = @.dbName + CAST(@.index as varchar)
exec ('select * from ' + @.dbName + '..table1')

remember dynamic SQLs r having security issues if not managed carefully|||thanks for replying. but i want to use 'use' syntax not 'exec'

this works fine
use test1
select * from table1

but when i use this i get the error
use 'test' + '1'
select * from table1|||Everything Upsalen has said is correct. His code is equivalent to what you requrest.

If you really must use USE then:
DECLARE @.dbName varchar(50)
DECLARE @.index int

SET @.index = 1
SET @.dbName ='test'

set @.dbName = @.dbName + CAST(@.index as varchar)
exec ('USE ' + @.dbname + ' GO select * from table1')|||The reason i want to use 'USE' is i have around 20 database (test1,test2,test3,...test20) with same schema, everytime when i make update or fix scripts i have to run it 20 times. now i want to use loop and run it once for all the databases.

here is what the code looks like...

DECLARE @.dbName varchar(50)
DECLARE @.index int
DECLARE @.NUM_OF_DB int

SET @.NUM_OF_DB = 20

SET @.dbName = 'test'
set @.index = 1

while @.index <= @.NUM_OF_DB
begin

set @.dbName = dbName + CAST(@.index as varchar)
USE @.dbName

-- paste here the scripts (script is thousands of lines long)

set @.index = @.index + 1
SET @.dbName = 'test'
end
or if you know easy way to do this plz ...|||yes, i know easy way to do this

if you have 20 databases which are essentially identical in structure (they would have to be if you can run the same thousand-line script on their tables), then just combine them into one database and vwalah, your USE problem goes away

:)|||Rudy's suggestion. My Code. Upsalen's code. Your code does not, and cannot, work. Unless perhaps you write something in .NET or similar and execute it from there. You cannot do what you want to do from QA\ SSMS.|||yes, i know easy way to do this

if you have 20 databases which are essentially identical in structure (they would have to be if you can run the same thousand-line script on their tables), then just combine them into one database and vwalah, your USE problem goes away

:)
c'mon......i have good reason not to do that|||okay, you have good reason

how about this: write 20 scripts, each with a different USE, each calling the same common script

:cool:|||You may be able to write some code using ADO\ ADO.NET - execute your script, looping through the various databases, changing the database at each pass.

I suspect that Rudy's point is that if you have 20 databases of all identical schemas then perhaps your design is flawed... Perhaps it isn't. But perhaps it is.|||You may be able to write some code using ADO\ ADO.NET - execute your script, looping through the various databases, changing the database at each pass.

I suspect that Rudy's point is that if you have 20 databases of all identical schemas then perhaps your design is flawed... Perhaps it isn't. But perhaps it is.

about design, 20 of them are same database. i just use them for training, testing,etc which means at the end it is one database.

where can i find code for ADO\ ADO.NET.|||were can i find code for ADO\ ADO.NET.You'll have to write it I am afraid. I don't have time to come up with any right now.|||I suspect that Rudy's point is that if you have 20 databases of all identical schemas then perhaps your design is flawed... Perhaps it isn't. But perhaps it is.well, yes, that was my point in post #6

but then, after having been assured that there is good reason for 20 databases, i put forth in post #9 a modest programming suggestion (not requiring ADD or whatever that was)

this suggestion, had it been undertaken, would have solved the problem elegantly

and about half an hour ago

:)|||well, yes, that was my point in post #6

but then, after having been assured that there is good reason for 20 databases, i put forth in post #9 a modest programming suggestion (not requiring ADD or whatever that was)

this suggestion, had it been undertaken, would have solved the problem elegantly

and about half an hour ago

:)
okey im just looking for easy way. how can i call common script?
to explain about db design i have one database copied 20 times for different purpose.|||ADO - ActiveX Data Objects
not to be confused with Data Access Objects which are totally different ;)

Yes - I missed the calling thingy - probably easier than ADO.|||another simple way to do this is to write a .bat file that calls osql.exe or sqlcmd.exe 20 times with the same script, each time connecting to a different database.|||another simple way to do this is to write a .bat file that calls osql.exe or sqlcmd.exe 20 times with the same script, each time connecting to a different database.
nice one i will give it a try|||Just wondering, does

DECLARE @.dbName varchar(50)
SET @.dbName = 'test'
USE @.dbName

Work?
If so, I might have a suggestion :p|||Just wondering, does

DECLARE @.dbName varchar(50)
SET @.dbName = 'test'
USE @.dbName

Work?
If so, I might have a suggestion :pNope :)|||another simple way to do this is to write a .bat file that calls osql.exe or sqlcmd.exe 20 times with the same script, each time connecting to a different database.
as your suggestion i write simple batch file and its working great just the way i want it. thank you men you saved me lot of time.
here is what the batch file looks like

SET YOGI_HOME=%cd%
SET /P IN_PUT_FILE=Script file name:
SET OUT_PUT_FILE=out.txt
SET NUM_DB=20
SET i=1
.
.
.

:Loop
Echo Executing script file %IN_PUT_FILE% on test%i%. . .
cd C:\Program Files\Microsoft SQL Server\80\Tools\Binn\
isqlw -s [Local] -d [test%i%] -E -u [sa] -p [password] -i [%YOGI_HOME%\%IN_PUT_FILE%] -o [%YOGI_HOME%\%OUT_PUT_FILE%]
.
.
.