all groups > sql server programming > september 2007 >
You're in the

sql server programming

group:

SQL 2005 Output Clause To VB6 Variable


SQL 2005 Output Clause To VB6 Variable Lovely Angel For You
9/8/2007 10:30:24 PM
sql server programming:
Hi,

I am using Visual basic 6 with SQL 2005. In one particular query, I
need to update 3 different tables, dependent on input.

So, the Primary ID for FirstTable, need to be refernced in other Two
Tables.

I searched a bit on the internet and found that SQL2005, has Output
Clause. However I am not sure how to bring the Inserted.ID to
VisualBasic 6 variable.

My Query is:

StrSQL = StrSQL & "INSERT INTO People (username, phone, city)
OutPut Inserted.ID AS CURID VALUES ("
StrSQL = StrSQL & "'" & LCase$(UserName.Text) & "', "
StrSQL = StrSQL & "'" & LCase$(phone.Text) & "', "
StrSQL = StrSQL & "'" & LCase$(city.Text) & "')"
CONN.Execute StrSQL
CONN.Close

Now how to get Inserted.ID to Visual Basic Variable. So the following
can be executed

VBVariable = Inserted.ID

If city.text = "MyCity" Then
StrSQL = StrSQL & "INSERT INTO Table2 (peopleid, zip, country)
VALUES ("
StrSQL = StrSQL & " & VBVariable & ", "
StrSQL = StrSQL & "'" & LCase$(zip.Text) & "', "
StrSQL = StrSQL & "'" & LCase$(country.Text) & "')"
CONN.Execute StrSQL
CONN.Close
End If

Any info on that will be highly appreciated.

Best Wishes
Lovely

________________________________________________________________________________

Posted In multiple groups as the issue is about two different
developement softwares.
Re: SQL 2005 Output Clause To VB6 Variable Uri Dimant
9/9/2007 12:00:00 AM
Hi
I have always used an OUTPUT clause to INSERT data into a staging/temporary
tables. From there you can easily SELECT the ID.However I assume your ID is
an INDENTITY property , right? If it is you can wrap your insert into stored
proccedure ( do that ASAP as you are under SQL Injection risk) and use
SELECT SCOPE_IDENTITY() to return ther value




[quoted text, click to view]

Re: SQL 2005 Output Clause To VB6 Variable Erland Sommarskog
9/9/2007 12:00:00 AM
Lovely Angel For You (lovely_angel_for_you@yahoo.com) writes:
[quoted text, click to view]

First of all, you need to learn to use parameterised statements. This
is essential for several reasons. What if there is a single quote in
any of the input fields? A naive will only get a syntax error. A
malicious user can use it to make the application run code you did
not intend it to. Parameterised statements also uses the plan cache
in SQL Server more efficiently.

I have a quick example on
http://www.sommarskog.se/dynamic_sql.html#SQL_injection, you will
need to scroll down a bit to see it.

As for using the OUTPUT clause, you could do:

INSERT People(username, phone, city)
OUTPUT Inserted.ID
VALUES (?, ?, ?)

In VB you run the query as it was a SELECT query. (Some books on client
programming appears to distinguish between action queries and read
queries, but that is hogwash. Your query is a typical example of one
that is both.) Thus:

SET rs = cmd.Execute StrSQL
VBVariable rs.Fields(0)
SET rs = Nothing

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: SQL 2005 Output Clause To VB6 Variable Bassam
9/9/2007 12:00:00 AM
Erland , If SQL injection is not an issue because this is not web
application and because i examine parameters in client carefully before
concatenating them in a string to construct the dynamic SQL would that still
be bad for the query plan? because i simply find Exec ContatenatedStringSQL
much practical than sp_executesql , its not just the parameter difference
that user might or might not select at client its also filter range between
them, example

table Orders and OrderDetails

TransactionDate (= , between , > , < , >= , <=) this applies to any date
column
StoreID (= , <>)
ItemStyle (= , Like , Not Like ) , applies to any varchar column
Qty (= , < , > , <= , >=) , applies to any numerical col

and so on, user might choose one or more from those parameters to filter
report output , AND might choose different filter ranges like described up
so you have many many possibilities, if all those possibilities are put in
the SP with executesql , it is simply too complex and time consuming but
constructing the SQL in client is far easier , i read your article about
dynamic SQL but still can't figure out how it can be easier to maintain and
construct ?! assuming SQL injection is not an issue as i said.

Thanks
Bassam

--

Thank you


[quoted text, click to view]

Re: SQL 2005 Output Clause To VB6 Variable Erland Sommarskog
9/9/2007 4:28:48 PM
Bassam (bassam@nileprojects.com) writes:
[quoted text, click to view]

SQL Injection can be an issue in Windows applications as well, not the
least if the application uses application roles or something else that
elevates privileges beyond the user's own.

[quoted text, click to view]

Which is a mess you don't have to deal with if you use parameterised
statements.

[quoted text, click to view]

The problem with inlined parameters is that each query will get its own
entry in the plan cache, so each query will be compiled, and the cache
will be filled with things that are never reused.

[quoted text, click to view]

But that does not preclude the use of parameterised statements. You can
still build the string dynamically, and this is often necessary when
the user needs to be able to search on different conditions.

[quoted text, click to view]

You may also want to read my article on dynamic search conditions,
http://www.sommarskog.se/dyn-search.html. You can compare the code
of search_orders_1 and search_orders_2 and see which one you find
cleaner. Yes, that is stored procedure, but the reasoning applies to
SQL generated in client code as well.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: SQL 2005 Output Clause To VB6 Variable Bassam
9/10/2007 12:00:00 AM
Thank you for your answers Erland, i will carfully read the article and
return to discussion if any
Bassam


[quoted text, click to view]

Re: SQL 2005 Output Clause To VB6 Variable Mark Yudkin
9/10/2007 8:07:14 PM
The OUTPUT clause in SQL2005 has absolutely nothing whatsoever to do with
retrieving data into an application. It is used to capture results into
another TABLE. To retrieve that information you can use a normal SELECT.

I suspect that what you actually want is to obtain the value of an IDENTITY
column, although you didn't say so. To do this, retrieve the result of
SELECT @@IDENTITY.

And finally, use ? parameter markers instead of creating code that is
demanding the launch of an SQL injection attack to cause havoc and
corruption.

[quoted text, click to view]

Re: SQL 2005 Output Clause To VB6 Variable Erland Sommarskog
9/10/2007 10:04:11 PM
Mark Yudkin (DoNotContactMe@boingboing.org) writes:
[quoted text, click to view]

Try this:

CREATE TABLE #temp (ident int IDENTITY,
alfa char(4) NOT NULL)
INSERT #temp (alfa)
OUTPUT inserted.ident
VALUES ('beta')
go
DROP TABLE #temp


[quoted text, click to view]

scope_identity() is better, in case the table you insert to have a trigger
which inserts into a second table with an identity column. @@identity
will give you the value for the second table.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
AddThis Social Bookmark Button