473,396 Members | 1,895 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,396 software developers and data experts.

Stored Procedure creation in vb.net

Wie
I need help.

How do I create a stored procedure with SQLCommand? I have tried following
string, but it doesn't work. I am using vs.net 2003 professional now.

dbCommand.CommandText = _
"CREATE PROCEDURE GetID" & vbCrLf & _
"@ID int out," & vbCrLf & _
"@Name varchar(20)" & vbCrLf & _
"AS" & vbCrLf & _
"select @ID = ID from Person where Name = @Name"

Thanks a lot.
Nov 21 '05 #1
3 1831
"Wie" <Wi*@discussions.microsoft.com> schrieb:
How do I create a stored procedure with SQLCommand?


Notice that a separate group for .NET+database questions is available. You
will more likely get an answer there:

<URL:news://news.microsoft.com/microsoft.public.dotnet.framework.adonet>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

Here's a snippet of some code for deleting/creating/using a couple of small
stored procedures

Hope it helps
'A) Create Table Proc

'remove old if exists first
SB.Remove(0, SB.Length)
SB.Append("IF EXISTS (SELECT ROUTINE_NAME")
SB.Append(" FROM INFORMATION_SCHEMA.ROUTINES")
SB.Append(" WHERE ROUTINE_TYPE = 'PROCEDURE' AND")
SB.Append(" ROUTINE_NAME = 'udp_CreateTable')")
SB.Append(" DROP PROCEDURE udp_CreateTable")
cmd.CommandText = SB.ToString
cmd.ExecuteNonQuery()

'Now Re/Create

SB.Remove(0, SB.Length)
SB.Append("CREATE PROCEDURE udp_CreateTable")
SB.Append(" @TableName nvarchar(50)")
SB.Append(" AS")
SB.Append(" Declare @SQL VarChar(1000)")
SB.Append(" SELECT @SQL = 'Create Table ' + @TableName + ' ('")
SB.Append(" SELECT @SQL = @SQL + ' [RecNum] int NOT NULL Primary
Key (RecNum))'")
SB.Append(" Exec (@SQL)")
cmd.CommandText = SB.ToString
cmd.ExecuteNonQuery()

'B) Create Insert RecNum Proc

'remove old if exists first
SB.Remove(0, SB.Length)
SB.Append("IF EXISTS (SELECT ROUTINE_NAME")
SB.Append(" FROM INFORMATION_SCHEMA.ROUTINES")
SB.Append(" WHERE ROUTINE_TYPE = 'PROCEDURE' AND")
SB.Append(" ROUTINE_NAME = 'udp_InsertRecNum')")
SB.Append(" DROP PROCEDURE udp_InsertRecNum")
cmd.CommandText = SB.ToString
cmd.ExecuteNonQuery()

'Now Re/Create
SB.Remove(0, SB.Length)
SB.Append("CREATE PROC udp_InsertRecNum")
SB.Append(" @TableName nvarchar(50),")
SB.Append(" @RecNum nvarchar(20)")
SB.Append(" AS")
SB.Append(" Declare @SQL VarChar(1000)")
SB.Append(" SELECT @SQL = 'INSERT INTO ' + @TableName + '
(RecNum)'")
SB.Append(" SELECT @SQL = @SQL + ' Values (' + @RecNum + ')'")
SB.Append(" Exec (@SQL)")
cmd.CommandText = SB.ToString
cmd.ExecuteNonQuery()
'use these stored procedures

'this creates table with single column: [RecNum] int NOT NULL

Private Sub udp_CreateTable(ByVal DataBaseNameForNewTable As String, ByVal
TableName As String)

cmdStats.CommandText = "EXEC udp_CreateTable [" &
DataBaseNameForNewTable & ".." & TableName & "]"
cmdStats.ExecuteNonQuery()

End Sub

'insert recnum to table
Private Sub udp_InsertRecNum(ByVal DataBase As String, ByVal TableName
As String, ByVal RecNum As Integer)

cmdStats.CommandText = "EXEC udp_InsertRecnum [" & DataBase & ".." &
TableName & "], " & RecNum
cmdStats.ExecuteNonQuery()

End Sub

"Wie" <Wi*@discussions.microsoft.com> wrote in message
news:5D**********************************@microsof t.com...
I need help.

How do I create a stored procedure with SQLCommand? I have tried
following
string, but it doesn't work. I am using vs.net 2003 professional now.

dbCommand.CommandText = _
"CREATE PROCEDURE GetID" & vbCrLf & _
"@ID int out," & vbCrLf & _
"@Name varchar(20)" & vbCrLf & _
"AS" & vbCrLf & _
"select @ID = ID from Person where Name = @Name"

Thanks a lot.

Nov 21 '05 #3
Wie
Thank you for your advice.
"Herfried K. Wagner [MVP]" wrote:
"Wie" <Wi*@discussions.microsoft.com> schrieb:
How do I create a stored procedure with SQLCommand?


Notice that a separate group for .NET+database questions is available. You
will more likely get an answer there:

<URL:news://news.microsoft.com/microsoft.public.dotnet.framework.adonet>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4

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

Similar topics

1
by: J. Muenchbourg | last post by:
I'm trying to create a stored procedure in vs.net, and following these instructions in my PrepLogic:XML WebServices Manual 5. Replace the boilerplate code in the Stored Procedure designer...
7
by: Douglas Buchanan | last post by:
I can't seem to open SQLS2k Stored Procedures in the IDE I am running MDE 2003 Version 7.1.3088 I have a MSDN professional subscription and did a complete install of vs.net Help explains how...
2
by: kanda | last post by:
Hello. I am developing the application (VBA&ODBC, to be exact) which periodically calls the stored procedures in the IBM DB2. A few of the procedures require executing with isolation level RR (...
3
by: comp_databases_ms-sqlserver | last post by:
This post is related to SQL server 2000 and SQL Server 2005 all editions. Many of my stored procedures create temporary tables in the code. I want to find a way to find the query plan for these...
2
by: GaryDean | last post by:
With ASP.Net 2.0 applications I am used to accessing data by using a 2.0 dataset and using the dataset wizard to create the tableadapter. I always (until now) have specified creation of embedded...
3
by: .Net Sports | last post by:
I need to call an stored procedure from an asp script, I don't need to do an Output parameter, only input parameters on 3 vars, but I get a "BOF EOF not true or record has been deleted" error when...
0
by: mersis | last post by:
I have a very very annoying problem. I want to create a stored procedure, that creates a table and does various things with it. Before creating a stored procedure I check if the table is there. If it...
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
11
by: peter | last post by:
I am trying to get a SQL stored procedure to use user maintained MQT implicitly which raises questions on when they are used or not used. In theory you would expect the stored procedure to pick up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...

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.