473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Creating temporary tables at run-time in SQL using VB.Net

Hi All :-)

Can anyone give me a URL where I can find a good example of code on how to
create a temporary SQL table using VB.net? I've checked the Microsoft site
at:

http://msdn.microsoft.com/library/de...es_04_8jtx.asp

which is okay at explaining the process but gives me absolutely no code
examples. Unfortunately, I am one of those programmers that can read and
read and read but still not "Get" it.... I need an example to go by, and
then I "Get" it.

Can any one please help?

Thanks so much :-)

Coleen
Nov 21 '05 #1
4 24127
Hi Coleen,

I do this sort of thing all the time, but generally I create tables that
persist. If I need to truncate them, I do so. Sometimes I just delete then
by calling one sp and then rebuild them with another.

Here's a simple example, creating the 'nhtable' table:
Dim ocmdnh As SqlCommand

ocmdnh = New SqlCommand("exec sp_makenhtable", oconn)

ocmdnh.ExecuteNonQuery()

'open nhtable

Dim danhtable As New SqlDataAdapter("select * from nhtable", oconn)

Dim dsnhtable As New DataSet("nhtable")

danhtable.Fill(dsnhtable, "nhtable")

the sp sp_makenhtable deletes the table and then rebuilds it, and then fills
it with data; in sql server it looks like this:

CREATE PROCEDURE sp_makenhtable AS
if exists (select * from information_schema.tables where table_name =
'nhtable')
drop table nhtable
select imcacct, brname, addr, addr2, city, st, zip, draw, bundlect,
issuecode into nhtable from manifest where draw < 300
Sometimes the build is more sophisiticated, adding primary keys, indices,
etc. Sometimes I even delete and dynamically build the sp itself. I will
do this when the sp has variable needs that are not easily passed into the
sp itself. In that case, I will call it to create it and then call it again
to exec it:

Dim ocmd As SqlCommand

ocmd = New SqlCommand("exec sp_dropsp_buildbranchlistandtable", oconn)

ocmd.ExecuteNonQuery()

Dim creationstring As String

If glf_custstat = "" Then

creationstring = "CREATE PROCEDURE sp_buildbranchlist AS "

creationstring += " select imcacct, brname, addr, addr2, city, st, zip,
custstat into branchlist from branches"

creationstring += vbCrLf & "where imcacct in " & longstring

Else

creationstring = "CREATE PROCEDURE sp_buildbranchlist AS "

creationstring += " select imcacct, brname, addr, addr2, city, st, zip,
custstat into branchlist from branches"

creationstring += vbCrLf & "where imcacct in " & longstring & " and custstat
= '" & glf_custstat & Chr(39)

End If

Dim sqladapt As New SqlDataAdapter

sqladapt.SelectCommand = New SqlCommand(creationstring, oconn)

Try

sqladapt.SelectCommand.ExecuteNonQuery()

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

ocmd = New SqlCommand("exec sp_buildbranchlist", oconn)

Try

ocmd.ExecuteNonQuery()

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

Let me know if you have questions about these approaches; I'll be glad to
help.

HTH,

Bernie Yaeger

"Coleen" <co**********@yaho.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...
Hi All :-)

Can anyone give me a URL where I can find a good example of code on how to
create a temporary SQL table using VB.net? I've checked the Microsoft site at:

http://msdn.microsoft.com/library/de...es_04_8jtx.asp
which is okay at explaining the process but gives me absolutely no code
examples. Unfortunately, I am one of those programmers that can read and
read and read but still not "Get" it.... I need an example to go by, and
then I "Get" it.

Can any one please help?

Thanks so much :-)

Coleen

Nov 21 '05 #2
Hi,

Here is some sample code on how to create a database, table, alter
table and
stored procedure.

Dim conn As SqlConnection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

strConn = "Server = " & Environment.MachineName

strConn += "\VSdotNET; Database = ; Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

conn.Open()

CreateDataBase()

CreateClientsTable()

End Sub

Private Sub CreateDataBase()

Dim strSQL As String

strSQL = "if Exists (Select * From master..sysdatabases Where Name = 'VET')"

strSQL += "DROP DATABASE VET" & vbCrLf & " CREATE DATABASE VET"

Dim cmd As New SqlCommand(strSQL, conn)

cmd.CommandType = CommandType.Text

Try

cmd.ExecuteNonQuery()

Catch

MessageBox.Show("Error Creating DB")

Finally

cmd.Dispose()

End Try

End Sub

Private Sub CreateClientsTable()

Me.Text = "Creating Clients Table..."

Dim strSQL As String = _

"USE VET" & vbCrLf & _

"IF EXISTS (" & _

"SELECT * " & _

"FROM VET.dbo.sysobjects " & _

"WHERE Name = 'Clients' " & _

"AND TYPE = 'u')" & vbCrLf & _

"BEGIN" & vbCrLf & _

"DROP TABLE VET.dbo.Clients" & vbCrLf & _

"END" & vbCrLf & _

"CREATE TABLE Clients (" & _

"ID Int NOT NULL," & _

"LastName NVarChar(20) NOT NULL," & _

"FirstName NVarChar(20) NOT NULL," & _

"Address NVarChar(150) NOT NULL," & _

"City NVarChar(20) NOT NULL," & _

"ZipCode NVarChar(5) NOT NULL," & _

"PhoneNumber NVarChar(20) NOT NULL," & _

"WorkNumber NVarChar(20)," & _

"CellNumber NVarChar(20)," & _

"Email NVarChar(50) NOT NULL," & _

"Balance Money NOT NULL," & _

"BalanceDate DateTime NOT NULL," & _

"CONSTRAINT [ID] PRIMARY KEY CLUSTERED" & _

"(ID))"

Dim cmd As New SqlCommand(strSQL, conn)

cmd.CommandType = CommandType.Text

Try

cmd.ExecuteNonQuery()

Catch ex As SqlException

MessageBox.Show(ex.ToString, "Clients")

Finally

cmd.Dispose()

End Try

End Sub

Private Sub MakeClientStoredProcedure()

Dim strSQL As String = _

"USE VET" & vbCrLf & _

"IF EXISTS (" & _

"SELECT * " & _

"FROM VET.dbo.sysobjects " & _

"WHERE Name = 'ClientInfo' " & _

"AND TYPE = 'p')" & vbCrLf & _

"BEGIN" & vbCrLf & _

"DROP PROCEDURE ClientInfo" & vbCrLf & _

"END"

Dim cmd As New SqlCommand(strSQL, conn)

cmd.CommandType = CommandType.Text

Try

cmd.ExecuteNonQuery()

cmd.CommandText = "Create Procedure ClientInfo" & vbCrLf & _

"@ClientID int " & vbCrLf & _

"AS Select * " & vbCrLf & _

"FROM VET.dbo.Clients Where ID = @ClientID"

cmd.ExecuteNonQuery()

Catch ex As SqlException

MessageBox.Show(ex.ToString, "Error Creating Stored Procedure")

Finally

cmd.Dispose()

End Try

End Sub

Alter table example
strSql = "ALTER TABLE PetInfo ADD Vet int NULL"

cmdUpdate = New SqlCommand(strSql, connVet)

connVet.Open()

cmdUpdate.ExecuteNonQuery()

connVet.Close()

Ken

-----------------------------

"Coleen" <co**********@yaho.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...
Hi All :-)

Can anyone give me a URL where I can find a good example of code on how to
create a temporary SQL table using VB.net? I've checked the Microsoft site
at:

http://msdn.microsoft.com/library/de...es_04_8jtx.asp

which is okay at explaining the process but gives me absolutely no code
examples. Unfortunately, I am one of those programmers that can read and
read and read but still not "Get" it.... I need an example to go by, and
then I "Get" it.

Can any one please help?

Thanks so much :-)

Coleen

Nov 21 '05 #3
Bernie, it's because of kind people like you & Cor (not to mention the
countless other who've helped me) that I appreciate this Newsgroup so much.
I guess you could say I'm a good Analyst/Designer, but when it comes to
writing code, I need examples, and I appreciate so much the help that I get!
THANKS!

"Bernie Yaeger" <be*****@cherwellinc.com> wrote in message
news:ep**************@TK2MSFTNGP11.phx.gbl...
Hi Coleen,

I do this sort of thing all the time, but generally I create tables that
persist. If I need to truncate them, I do so. Sometimes I just delete then by calling one sp and then rebuild them with another.

Here's a simple example, creating the 'nhtable' table:
Dim ocmdnh As SqlCommand

ocmdnh = New SqlCommand("exec sp_makenhtable", oconn)

ocmdnh.ExecuteNonQuery()

'open nhtable

Dim danhtable As New SqlDataAdapter("select * from nhtable", oconn)

Dim dsnhtable As New DataSet("nhtable")

danhtable.Fill(dsnhtable, "nhtable")

the sp sp_makenhtable deletes the table and then rebuilds it, and then fills it with data; in sql server it looks like this:

CREATE PROCEDURE sp_makenhtable AS
if exists (select * from information_schema.tables where table_name =
'nhtable')
drop table nhtable
select imcacct, brname, addr, addr2, city, st, zip, draw, bundlect,
issuecode into nhtable from manifest where draw < 300
Sometimes the build is more sophisiticated, adding primary keys, indices,
etc. Sometimes I even delete and dynamically build the sp itself. I will
do this when the sp has variable needs that are not easily passed into the
sp itself. In that case, I will call it to create it and then call it again to exec it:

Dim ocmd As SqlCommand

ocmd = New SqlCommand("exec sp_dropsp_buildbranchlistandtable", oconn)

ocmd.ExecuteNonQuery()

Dim creationstring As String

If glf_custstat = "" Then

creationstring = "CREATE PROCEDURE sp_buildbranchlist AS "

creationstring += " select imcacct, brname, addr, addr2, city, st, zip,
custstat into branchlist from branches"

creationstring += vbCrLf & "where imcacct in " & longstring

Else

creationstring = "CREATE PROCEDURE sp_buildbranchlist AS "

creationstring += " select imcacct, brname, addr, addr2, city, st, zip,
custstat into branchlist from branches"

creationstring += vbCrLf & "where imcacct in " & longstring & " and custstat = '" & glf_custstat & Chr(39)

End If

Dim sqladapt As New SqlDataAdapter

sqladapt.SelectCommand = New SqlCommand(creationstring, oconn)

Try

sqladapt.SelectCommand.ExecuteNonQuery()

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

ocmd = New SqlCommand("exec sp_buildbranchlist", oconn)

Try

ocmd.ExecuteNonQuery()

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

Let me know if you have questions about these approaches; I'll be glad to
help.

HTH,

Bernie Yaeger

"Coleen" <co**********@yaho.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...
Hi All :-)

Can anyone give me a URL where I can find a good example of code on how to create a temporary SQL table using VB.net? I've checked the Microsoft

site
at:

http://msdn.microsoft.com/library/de...es_04_8jtx.asp

which is okay at explaining the process but gives me absolutely no code
examples. Unfortunately, I am one of those programmers that can read and read and read but still not "Get" it.... I need an example to go by, and then I "Get" it.

Can any one please help?

Thanks so much :-)

Coleen


Nov 21 '05 #4
Again, Thank you, thank you for all the help I've been getting!

Coleen :-)

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uN****************@TK2MSFTNGP15.phx.gbl...
Hi,

Here is some sample code on how to create a database, table, alter
table and
stored procedure.

Dim conn As SqlConnection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

strConn = "Server = " & Environment.MachineName

strConn += "\VSdotNET; Database = ; Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

conn.Open()

CreateDataBase()

CreateClientsTable()

End Sub

Private Sub CreateDataBase()

Dim strSQL As String

strSQL = "if Exists (Select * From master..sysdatabases Where Name = 'VET')"
strSQL += "DROP DATABASE VET" & vbCrLf & " CREATE DATABASE VET"

Dim cmd As New SqlCommand(strSQL, conn)

cmd.CommandType = CommandType.Text

Try

cmd.ExecuteNonQuery()

Catch

MessageBox.Show("Error Creating DB")

Finally

cmd.Dispose()

End Try

End Sub

Private Sub CreateClientsTable()

Me.Text = "Creating Clients Table..."

Dim strSQL As String = _

"USE VET" & vbCrLf & _

"IF EXISTS (" & _

"SELECT * " & _

"FROM VET.dbo.sysobjects " & _

"WHERE Name = 'Clients' " & _

"AND TYPE = 'u')" & vbCrLf & _

"BEGIN" & vbCrLf & _

"DROP TABLE VET.dbo.Clients" & vbCrLf & _

"END" & vbCrLf & _

"CREATE TABLE Clients (" & _

"ID Int NOT NULL," & _

"LastName NVarChar(20) NOT NULL," & _

"FirstName NVarChar(20) NOT NULL," & _

"Address NVarChar(150) NOT NULL," & _

"City NVarChar(20) NOT NULL," & _

"ZipCode NVarChar(5) NOT NULL," & _

"PhoneNumber NVarChar(20) NOT NULL," & _

"WorkNumber NVarChar(20)," & _

"CellNumber NVarChar(20)," & _

"Email NVarChar(50) NOT NULL," & _

"Balance Money NOT NULL," & _

"BalanceDate DateTime NOT NULL," & _

"CONSTRAINT [ID] PRIMARY KEY CLUSTERED" & _

"(ID))"

Dim cmd As New SqlCommand(strSQL, conn)

cmd.CommandType = CommandType.Text

Try

cmd.ExecuteNonQuery()

Catch ex As SqlException

MessageBox.Show(ex.ToString, "Clients")

Finally

cmd.Dispose()

End Try

End Sub

Private Sub MakeClientStoredProcedure()

Dim strSQL As String = _

"USE VET" & vbCrLf & _

"IF EXISTS (" & _

"SELECT * " & _

"FROM VET.dbo.sysobjects " & _

"WHERE Name = 'ClientInfo' " & _

"AND TYPE = 'p')" & vbCrLf & _

"BEGIN" & vbCrLf & _

"DROP PROCEDURE ClientInfo" & vbCrLf & _

"END"

Dim cmd As New SqlCommand(strSQL, conn)

cmd.CommandType = CommandType.Text

Try

cmd.ExecuteNonQuery()

cmd.CommandText = "Create Procedure ClientInfo" & vbCrLf & _

"@ClientID int " & vbCrLf & _

"AS Select * " & vbCrLf & _

"FROM VET.dbo.Clients Where ID = @ClientID"

cmd.ExecuteNonQuery()

Catch ex As SqlException

MessageBox.Show(ex.ToString, "Error Creating Stored Procedure")

Finally

cmd.Dispose()

End Try

End Sub

Alter table example
strSql = "ALTER TABLE PetInfo ADD Vet int NULL"

cmdUpdate = New SqlCommand(strSql, connVet)

connVet.Open()

cmdUpdate.ExecuteNonQuery()

connVet.Close()

Ken

-----------------------------

"Coleen" <co**********@yaho.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...
Hi All :-)

Can anyone give me a URL where I can find a good example of code on how to
create a temporary SQL table using VB.net? I've checked the Microsoft site at:

http://msdn.microsoft.com/library/de...es_04_8jtx.asp
which is okay at explaining the process but gives me absolutely no code
examples. Unfortunately, I am one of those programmers that can read and
read and read but still not "Get" it.... I need an example to go by, and
then I "Get" it.

Can any one please help?

Thanks so much :-)

Coleen

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Soefara | last post by:
Dear Sirs, I have been developing an application on Windows with MySQL 3.23, making use of temporary tables. Now when I try to port the application to a Unix box running also MySQL 3.23, I...
1
by: Chetanwarade | last post by:
Hi, I have one problem? I want to know if I have to show all records from table on ASP page.Should I use temporary table? Also where to use temporary tables? Thank you
30
by: btober | last post by:
Whenever I create a temporary table, with something like CREATE TEMPORARY TABLE temptable1 AS SELECT * FROM paid.ad_hoc_query; New schemas appear, with names like "pg_temp_1". I guess the...
2
by: David Parker | last post by:
In our application we have a table that tracks network sessions. The usage is: 1) create a session record 2) read/update the record several times during the session 3) delete the session record...
12
by: Joachim Pense | last post by:
Is there recommended way to get the execution plan for queries involving global temporary tables (from the UNIX command line or within a script)? I run the queries in Perl scripts, and the only...
1
by: phil | last post by:
I have been developing some queries which will list the dependencies of Stored Procedures (this is with DB2/UDB v8). In doing so, I have noticed that dependencies are ignored if they occur in an...
0
by: msbhaskar | last post by:
I got to know that we can create indexes on declared global temporary tables in DB2, but not on the created temporary tables. For my project, i need urgent inputs on this to how-to achieve this in...
4
by: john | last post by:
When leaving the main menu form I want to delete some temporary tables. The following code on the form's close works: DoCmd.DeleteObject acTable, "tmpEnvelop". But is it possible to change it so...
1
by: popwhub | last post by:
I am having problems printing some reports from my application that are generated from temporary tables. The application has an Access FE and a SQL Server BE. The current method uses a temporary...
10
by: w.l.fischer | last post by:
Is it possible to have statistics on temporary tables? I frequently put 10000 or more rows in a temporary table and would like to know it the queries become faster with statistics on those tables.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.