Creating a SQL Server stored procedure in VB.NET - sql-server

How to create a SQL Server stored procedure in VB.NET by using Imports System.Data.SqlClient?
Public Function createstbkdb()
Return "CREATE PROCEDURE test4444 " + _
+"AS" + _
+"GO"
End Function
Regards,

You can use SqlCommand.ExecuteNonQuery(), passing in the text of your procedure (as queryString in the linked MSDN example).
Note that the connection executing the VB.NET code must have appropriate permissions in the database to create the procedure.
UPDATE
As #CmlJame noted, leave out the "GO". That is meaningful to SSMS but should not be included in the call to ExecuteNonQuery().

Related

Dynamic connection string in SSIS

I am having the following problem with populating a dynamic file path for an XML file from a SSIS variable.
In Visual Studio 2017 I have an Execute SQL Task with a MS SQL stored procedure that returns two columns. The first column is a date (stored as a string) and the second is a URL. These two columns (single row) populate SSIS variables and are mapped on the Result Set tab of the Script Task. The next step is script task that uses the URL from the variable to download an xml file from a web service. The xml file is stored using a file Connection Manager. The connection string for the file is an expression that should be using the 1st variable (User::rateDate) from the Execute SQL Task
Connection String expression:
#[User::xmlFileLocation] + "ExchangeRates-" + #[User::rateDate] + ".xml"
This evaluates to
\server\ExchangeRates\ExchangeRates-.xml
XML file should be saved as ExchangeRates-2017-12-19.xml with 2017-12-19 being the result of the stored procedure, but instead the XML file is saved as ExchangeRates-.xml
If I manually populate the User::rateDate variable it will use that in the Connection String, but I haven't been able to get it to populate from the stored procedure result.
The date generated is part of the URL generation too so I want this both created in the same place, i.e. I don't want to assign the file name via some GETDATE() logic in the expression.
I have confirmed the variable is being populated is a Script Task C# pop up.
I have confirmed that it is not a date/string issue by changing the stored procedure result to an explicit string, like "test". It still doesn't get added to the Connection String.
Thanks, Tim
I will provide 2 solutions based on the Stored Procedure type:
Stored Procedure with a Select Statement
I will assume that you are using a Stored procedure that contains a SELECT statement that return a Table of 2 columns: ServerURL and rateDate
In this case you have to insert the result of this select statement into a temp table, then read from these temp table, as example:
CREATE TABLE #TBL(ServerURL varchar(4000), rateDate varchar(50))
INSERT INTO #TBL EXEC pr_rateDate
SELECT TOP 1 * FROM #TBL
In this way your variables mapping should work
Stored Procedure with Output Parameters
I will assume that you are using a stored procedure which require 2 output parameters to be passed, example:
EXEC sp_rateDate #ServerURL OUTPUT, #rateDate OUTPUT
So you have to use the following SQL statemment:
EXEC sp_rateDate ? OUTPUT, ? OUTPUT
and you have to add 2 output parameters in the Parameter Mapping tab
Useful Links
Parameters and Return Codes in the Execute SQL Task Read Using Parameters with Stored Procedures part
SQL Assist - SSIS Execute SQL Task
Map Result Sets to Variables in an Execute SQL Task
How to Execute Stored Procedure in SSIS Execute SQL Task in SSIS
SSIS Basics: Using the Execute SQL Task to Generate Result Sets
Change the datatype of variable User::rateDate to datetime. Then, change your connection string expression to the following:
#[User::xmlFileLocation] +
"ExchangeRates-" +
(DT_WSTR,4)DATEPART("yyyy",#[User::rateDate]) +
RIGHT("0" + (DT_WSTR,2)DATEPART("mm",#[User::rateDate]) ,2) +
RIGHT("0" + (DT_WSTR,2)DATEPART("dd",#[User::rateDate]),2) +
".xml"

SQL Server : Create Procedure in DB without using [GO]

I want to execute this (simplified) query using node-mssql that executes in SQL Server 2017 fine:
USE [Journal]
[GO]
CREATE PROCEDURE [dbo].[EventDelete]
#NotificationID INT
AS
DELETE Notification
WHERE NotificationID = #NotificationID
[GO]
node-mssql declares syntax error using [GO] and requires semicolon, therefore I try this:
USE [Journal];
CREATE PROCEDURE [dbo].[EventDelete]
#NotificationID INT
AS
DELETE Notification
WHERE NotificationID = #NotificationID;
Now we get error:
CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
So let's try this:
CREATE PROCEDURE [Journal].[dbo].[EventDelete]
#NotificationID INT
AS
DELETE Notification
WHERE NotificationID = #NotificationID;
Now we get
RequestError: 'CREATE/ALTER PROCEDURE' does not allow specifying the database name as a prefix to the object name.
Naturally without any DB declaration it attempts to attach to the master error:
CREATE PROCEDURE permission denied in database 'master'.
So writing the question really works at setting one's thoughts straight.
The reason is the stored procedure requires to be created in one batch, which [GO] signifies, with nothing else.
Execute USE [Journal] as one batch using the .batch('USE [Journal]') method and then the SQL to CREATE PROCEDUCE as a new .batch(...) execution, sequentially.
Unless there is another method within node-mssql which allows for multi-batch executions?

Calling a SQL Server stored procedure from Visual Foxpro 9

I have been trying to call a SQL Server stored procedure that has no parameters and no return values. All it does is recalculate data in a SQL Server database.
I thought I could use something simple like
lsqlcmd = " execute storedprocname"
but the procedure is not being called and I am not receiving an errors.
Any suggestions?
Can you try calling it in SQLEXEC()? This how I've seen it done:
TEXT TO lcSQLCommand
<database>.<schema>.<sproc>
ENDTEXT
gcConnectionString = [Driver={SQL Server Native Client 10.0};Server=] + "<servername>" + [;Database=] + "<database>" + [;Trusted_Connection=yes]
STORE SQLSTRINGCONNECT(gcConnectionString) TO gnConnHandle
SQLEXEC(gnConnHandle, lcSQLCommand)
You'll need to update the connection string to however your database is configured, this was for windows authentication.

How to call a stored procedure imported from a database using add new datasource

In my project I went on Data source window and click add new, and I imported a stored procedure with 3 parameters (pivot table stored procedure).
In Solution Explorer appear a data set, with double click on this data set I open it and see my procedure there like a QueryTableAdapter.
I want to run this procedure but I do not know how.
The procedure is functional, the database is made in SQL Server, I want to run it in Visual Basic.
My questions are:
Why a simple procedure with parameters is imported like table and you can drag & drop on a form from datasource and a stored procedure for pivot table is not imported like table?
How can I run an pivot table stored procedure imported in the project by using "Add New Datasource" from database.
Stored procedures that do not return rows are imported into a query adapter.
It's like a tableadapter specifically for functions that return a single value.
dim Adapter as new YourdatabasenamequryAdapter
'YourFunction is a method inside the query adapter
Dim x as boolean = YourdatabasenamequryAdapter.yourFunction(YourParams)
Any procedure that that returns multiple rows will import as a table.
Dim table as new yourprocedureTable ' heres a table to fill
Dim adapter as new yourprocedureadapter ' here is a class for invoking you 'procedure
'to run the proceedure
adapter.fill(table,param1,param2...) 'You give it a table to fill and call the 'fill method.

VB.Net Upgrade Stored Procedure Call to Oracle Database moRs.Open(moCmd)

I have a program that was written in VB6 and we are trying to convert to VB.Net. When calling stored procedures in Microsoft SQL 2012 and in Oracle using this .Open() method, Microsoft SQL executes fine while Oracle does not. Here is a snippet of code:
Private moRs As ADORecordSetHelper = New ADORecordSetHelper("")
Private moCmd As DbCommand 'where moCmd has the procedure call and parameters
moRs.Open(moCmd)
Here is the definition of .Open()
Public Sub Open(command As System.Data.Common.DbCommand)
Member of UpgradeHelpers.DB.ADO.ADORecordSetHelper
This all works for Microsoft SQL just not with Oracle. I found execute moCmd.ExecuteNonQuery() will work execute the stored procedure, but I need to get the recordset in certain instances as a result.
Is there a way to use moRs.Open(moCmd) for both Microsoft and Oracle? If not, how could I accomplish the same task of getting back the recordsets?
UPDATE: I have figured out that the .Open() method uses Execute Reader which is working fine for the Microsoft SQL that strictly returns a recordset. The Oracle stored procedure is inserting rows and returning values from the procedure. Doing an Execute NonQuery on the same object (moCmd) executes just fine, but all I am able to get from this method is the number of rows affected and not the other information that I need. Thoughts on why the Execture Reader doesn't work? My search says that it should work for all types of SQL; insert, delete, update, etc.

Resources