Can Variables be plugged into SQL statements??

Can Variables be plugged into SQL statements??

Post by Ken Sturge » Wed, 19 Mar 1997 04:00:00



How would one go about using variable names in SQL statements? Is it possible?

Thanks for helping:-)

Ken Sturgeon

http://www.inlink.com/~fishhead

 
 
 

Can Variables be plugged into SQL statements??

Post by Joe Garri » Wed, 19 Mar 1997 04:00:00



>How would one go about using variable names in SQL statements? Is it possible?

Just like you would for any other string that you build in code:

Function SelectFromAnyTable(sTableName As String) As String
  SelectFromAnyTable = "SELECT * FROM " & sTableName
End Function

Did I misunderstand the question?

Joe

Never underestimate the power of a WAG.

http://www.citilink.com/~jgarrick/vbasic/

 
 
 

Can Variables be plugged into SQL statements??

Post by Space Invade » Thu, 20 Mar 1997 04:00:00



> How would one go about using variable names in SQL statements? Is it possible?
> Thanks for helping:-)
> Ken Sturgeon

> http://www.inlink.com/~fishhead

I don't think so, but their values can be used to build SQL
statements, or they can be used to populate a QueryDef's
parameters. Here are examples off the top of my head:

Dim SQL As String: SQL = "select * from people where name = """ Me!Text1.Text & """;"
Set MyRecordset = MyDB.OpenRecordset(SQL)

Dim QD As QueryDef: Set QD = MyDB.OpenQueryDef("my query")
QD!Param1 = Me!Text1.Text
Set MyRecordset = QD.OpenRecordset()

--

WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!

 
 
 

Can Variables be plugged into SQL statements??

Post by Mike Nadea » Fri, 21 Mar 1997 04:00:00


Do you mean like SQL = "SELECT * FROM PEOPLE WHERE [LAST NAME] = ' " &
acLastName & " ' "
or do you mean Named Variables?



> How would one go about using variable names in SQL statements? Is it
possible?

> Thanks for helping:-)

> Ken Sturgeon

> http://www.inlink.com/~fishhead

 
 
 

Can Variables be plugged into SQL statements??

Post by Robert D. Baird, » Mon, 31 Mar 1997 04:00:00


Yes, you can put variable values into an SQL statement.  Here is some
sample code.

Important - You must embed all text in single quotes

Note:
        myDatabase must be opened prior to executing the SQL
        Field1 is a string
        Field2 is an integer

Dim myVar as String
Dim myInt as Integer
Dim SQL$

myVar = "ABCD"
myInt = 20

SQL$ = "Update Table Set Field1='" & myVar & "' Where Field1='DEFG'"
myDatabase.Execute SQL$

SQL$ = "Update Table Set Field2=" & myInt & " Where Field2=10"
myDatabase.Execute SQL$

Hope this helps

Rob.



>How would one go about using variable names in SQL statements? Is it possible?

>Thanks for helping:-)

>Ken Sturgeon

>http://www.inlink.com/~fishhead

 
 
 

Can Variables be plugged into SQL statements??

Post by Nimesh Sha » Tue, 01 Apr 1997 04:00:00


I am not sure if this is what you are looking for:

SELECT "MyVarName" = MyTable.MyColumnName FROM MyTable



> Yes, you can put variable values into an SQL statement.  Here is some
> sample code.

> Important - You must embed all text in single quotes

> Note:
>    myDatabase must be opened prior to executing the SQL
>    Field1 is a string
>    Field2 is an integer

> Dim myVar as String
> Dim myInt as Integer
> Dim SQL$

> myVar = "ABCD"
> myInt = 20

> SQL$ = "Update Table Set Field1='" & myVar & "' Where Field1='DEFG'"
> myDatabase.Execute SQL$

> SQL$ = "Update Table Set Field2=" & myInt & " Where Field2=10"
> myDatabase.Execute SQL$

> Hope this helps

> Rob.



> >How would one go about using variable names in SQL statements? Is it
possible?

> >Thanks for helping:-)

> >Ken Sturgeon

> >http://www.inlink.com/~fishhead

 
 
 

Can Variables be plugged into SQL statements??

Post by Bud Cookso » Thu, 03 Apr 1997 04:00:00


If you use RDO, it is possible to create a "parameter" query that allows
you to have replacable parameters.  In fact, you use the rdoParameters
collection to set the values of the parameters for each execution.  See the
subject "Creating Parameter Queries" in the VB help.       BUD



> Yes, you can put variable values into an SQL statement.  Here is some
> sample code.

> Important - You must embed all text in single quotes

> Note:
>    myDatabase must be opened prior to executing the SQL
>    Field1 is a string
>    Field2 is an integer

> Dim myVar as String
> Dim myInt as Integer
> Dim SQL$

> myVar = "ABCD"
> myInt = 20

> SQL$ = "Update Table Set Field1='" & myVar & "' Where Field1='DEFG'"
> myDatabase.Execute SQL$

> SQL$ = "Update Table Set Field2=" & myInt & " Where Field2=10"
> myDatabase.Execute SQL$

> Hope this helps

> Rob.



> >How would one go about using variable names in SQL statements? Is it
possible?

> >Thanks for helping:-)

> >Ken Sturgeon

> >http://www.inlink.com/~fishhead

 
 
 

1. Plugging variables into a select statement

Two possibilities:


SELECT * FROM mytable
SET ROWCOUNT 0

or


If you're going to go the EXEC route, please read
http://www.algonet.se/~sommar/dynamic_sql.html

(This specific case is discussed at
http://www.algonet.se/~sommar/dynamic_sql.html#Top )

--
Aaron Bertrand, SQL Server MVP
http://www.aspfaq.com/

Please reply in the newsgroups, but if you absolutely
must reply via e-mail, please take out the TRASH.


2. Looking for dBase ODBC Drivers

3. I am trying to store the result of a dynamic sql query in a variable

4. Oracle<->Informix

5. Plug-In Azuim Variables ???

6. MA-Cambridge-98790--ORACLE-DBA Skills-Oracle DBA

7. I am having a problem with the following SQL statements:

8. screens not generating when generating projects

9. ? Excuting a variable or variable columns in a create statement

10. Set a variable to equal a select statement containing variables

11. Can you pass a where with multiple statements without a variable for each where statement

12. Global variable as SQL statement on oracle connection