I am creating a WPF application using Advantage database server. I want to insert some data using stored procedure
Any sample code ?
I tried two input parameter TestID and TestName (both NCHAR)
INSERT INTO TestTable(
Test_Id,
Test_Name)
VALUES (
#TestID,
#TestName);
But show error like
Error 7200: AQE Error: State = HY000; NativeError = 5154;
[SAP][Advantage SQL Engine][ASA] Error 5154: Execution of the stored
procedure failed. Procedure Name: TestInsert. Error 7200: AQE
Error: State = S0000; NativeError = 2121; [SAP][Advantage SQL
Engine]Column not found: #TestID -- Location of error in the SQL
statement is: 42 (line: 3 column: 5) Error in stored procedure:
TestInsert AdsCommand query execution failed.
I am new in SAP ADS. Please help me.
use _XXXX notation for input parameters.
ie,
INSERT INTO TestTable( Test_Id, Test_Name)
VALUES ( _#TestID, _#TestName);
Using BizTalk I am trying to insert/update the table in the SQL Server database using the stored procedure. I have created a stored procedure and the Table Type like below
CREATE TYPE dbo.dept_TT AS TABLE
(
dept_name varchar(64),
jax_dept_id char(32)
)
GO
CREATE PROCEDURE [dbo].[uspInsertorUpdateDept]
#dept_TT dept_TT READONLY
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRANSACTION;
UPDATE dep
SET dep.dept_name = dtt.dept_name,
dep.jax_dept_id = dtt.jax_dept_id
FROM [afm].[jax_dept] dep
INNER JOIN #dept_TT dtt ON dep.jax_dept_id = dtt.jax_dept_id
INSERT INTO [afm].[jax_dept](dept_name, jax_dept_id )
SELECT dtt.dept_name, dtt.jax_dept_id
FROM #dept_TT dtt
WHERE NOT EXISTS (SELECT 1
FROM [afm].[jax_dept]
WHERE jax_dept_id = dtt.jax_dept_id)
COMMIT TRANSACTION;
END;
When I execute the stored produre in the SQL Server management studio it insert/updates the records as expected. I am consuming this storedprocedure in the biztalk application and tried to run the application it throws error like
The adapter failed to transmit message going to send port "WcfSendPort_SqlAdapterBinding_Procedures_dbo_Custom_Dep" with URL "mssql://". It will be retransmitted after the retry interval specified for this Send Port. Details:"Microsoft.ServiceModel.Channels.Common.XmlReaderParsingException: An unexpected method call was made. Ensure that the XML is well formed. The stack trace of the method call was : Void WriteFullEndElement().
I enabled the tracking and tried seeing the XML that is sent to the send port and it looks good like below.
<?xml version="1.0" encoding="utf-8"?>
<ns0:uspInsertorUpdateDept xmlns:ns0="http://schemas.microsoft.com/Sql/2008/05/Procedures/dbo" xmlns:ns4="http://schemas.datacontract.org/2004/07/System.Data" xmlns:ns3="http://schemas.microsoft.com/Sql/2008/05/Types/TableTypes/dbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns0:dept_TT>
<ns3:dept_TT>
<ns3:dept_name>lab1</ns3:dept_name>
<ns3:jax_dept_id>RRI</ns3:jax_dept_id>
</ns3:dept_TT>
<ns3:dept_TT>
<ns3:dept_name>lab2</ns3:dept_name>
<ns3:jax_dept_id>RAFAC</ns3:jax_dept_id>
</ns3:dept_TT>
</ns0:dept_TT>
</ns0:uspInsertorUpdateDept>
Xml generated for the stored procedure in the VS
<ns0:uspInsertorUpdateDept xmlns:ns0="http://schemas.microsoft.com/Sql/2008/05/Procedures/dbo">
<ns0:dept_TT>
<ns1:dept_TT xmlns:ns1="http://schemas.microsoft.com/Sql/2008/05/Types/TableTypes/dbo">
<ns1:dept_name>dept_namedept_namedept_namedept_namedept_namedept_namedept_named</ns1:dept_name>
<ns1:jax_dept_id>jax_dept_idjax_dept_idjax_dept_i</ns1:jax_dept_id>
</ns1:dept_TT>
<ns1:dept_TT xmlns:ns1="http://schemas.microsoft.com/Sql/2008/05/Types/TableTypes/dbo">\
<ns1:dept_name>dept_namedept_namedept_namedept_namedept_namedept_namedept_named</ns1:dept_name>
<ns1:jax_dept_id>jax_dept_idjax_dept_idjax_dept_i</ns1:jax_dept_id>
</ns1:dept_TT>
<ns1:dept_TT xmlns:ns1="http://schemas.microsoft.com/Sql/2008/05/Types/TableTypes/dbo">
<ns1:dept_name>dept_namedept_namedept_namedept_namedept_namedept_namedept_named</ns1:dept_name>
<ns1:jax_dept_id>jax_dept_idjax_dept_idjax_dept_i</ns1:jax_dept_id>
</ns1:dept_TT>
</ns0:dept_TT>
</ns0:uspInsertorUpdateDept>
Not sure what am I missing here. Any help is greatly appreciated
I am trying to use GO to get R to pull a multipart query from a SQL Server database but R keeps erroring out on me when I try this. Does anyone know a workaround to get RODBC to run multipart queries?
Example query:
query2 = "IF OBJECT_ID('tempdb..#ATTTempTable') IS NOT NULL
DROP TABLE #ATTTempTable
GO
SELECT
* INTO #ATTTempTable
FROM ETL.ATT.fact_responses fr
WHERE fr.ResponseDateTime > '2015-07-06'
"
channel <- odbcConnect("<host name>", uid="<uid>", pwd="<pwd>")
raw = sqlQuery(channel, query2)
close(channel)
and result
> raw
[1] "42000 102 [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near 'GO'."
[2] "[RODBC] ERROR: Could not SQLExecDirect 'IF OBJECT_ID('tempdb..#ATTTempTable') IS NOT NULL\n DROP TABLE #ATTTempTable\n\nGO\n\nSELECT\n\t* INTO #ATTTempTable\nFROM ETL.ATT.fact_responses fr\nWHERE fr.ResponseDateTime > '2015-07-06'\n'"
>
Because your query contains multiple line with conditional logic it resembles a stored procedure.
Simply save that stored procedure in SQL Server:
CREATE PROCEDURE sqlServerSp #ResponseDateTime nvarchar(10)
AS
IF OBJECT_ID('tempdb..#ATTTempTable') IS NOT NULL
DROP TABLE #ATTTempTable;
GO
-- suppresses affected rows message so RODBC returns a dataset
SET NO COUNT ON;
GO
-- runs make-table action query
SELECT * INTO #ATTTempTable
FROM ETL.ATT.fact_responses fr
WHERE fr.ResponseDateTime > #ResponseDateTime;
GO
And then run the stored procedure in R. You can even pass parameters like the date:
channel <- odbcConnect("<host name>", uid="<uid>", pwd="<pwd>")
raw = sqlQuery(channel, "EXEC sqlServerSp #ResponseDateTime='2015-07-06'")
close(channel)
You can't. See https://msdn.microsoft.com/en-us/library/ms188037.aspx
you have to divide your query into two statements and run them separately.
I have a stored procedure in a Microsoft SQL Server database:
USE [ProjectIndexer]
GO
/****** Object: StoredProcedure [files].[add_file] Script Date: 12/12/2014 1:34:20 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [files].[add_file]
#FILENAME varchar(255),
#EXTENSION nvarchar(8),
#PATH_FULL varchar(255),
#RELATIVE_PATH varchar(255),
#JOB varchar(15),
#DATE_MODIFIED datetimeoffset(7),
#SIZE BIGINT,
#INDEX_MODULE INT,
#FILE_TYPE varchar(255),
#DOC_TYPE varchar(255),
#DISCIPLINE varchar(255)
AS
DECLARE #file_id sql_variant
--Insert values for new file
INSERT INTO files.GENERAL
(FILENAME, EXTENSION, PATH_FULL, RELATIVE_PATH, JOB, DATE_MODIFIED, SIZE, INDEX_MODULE, FILE_TYPE, DOC_TYPE, DISCIPLINE)
VALUES(#FILENAME, #EXTENSION, #PATH_FULL, #RELATIVE_PATH, #JOB, #DATE_MODIFIED, #SIZE, #INDEX_MODULE, #FILE_TYPE, #DOC_TYPE, #DISCIPLINE);
--Get the ID of this new file
SELECT #file_id = current_value FROM sys.sequences WHERE name = 'FILE_ID_SEQ';
--Return ID
RETURN CONVERT(bigint, #file_id)
I am trying to run this procedure in a VB application developed in Visual Studio 2012, using table adapters:
Dim myFilesGeneralTableAdapter As New FILES_GeneralTableAdapter
Dim FileID As Int32
FileID = myFilesGeneralTableAdapter.AddFile(FileName, fileExtension, foundFile, fileRelativePath, JobNumber, fileDateModified, fileSize, Nothing, Nothing, Nothing, Nothing)
For some reason, the function isn't returning the value to the VB variable 'FileID'. I can, however, use the "Preview Data" feature in the dataset designer to insert values for the above parameters in Visual Studio, and in that environment I'm able to get a returned value.
This suggests to me that my syntax in my VB module is wrong. Can anyone tell me what the error is?
Have you considered using System.Data.SqlClient.SqlCommand? You can use it to call your stored procedure, add the parameter values, and execute it in such a way that you retrieve a single value, i.e.
Try
'Create command'
Dim command As New SqlCommand
command.CommandType = CommandType.StoredProcedure
conn = New SqlConnection(myConnString)
command.Connection = conn
command.CommandText = "add_file"
command.Parameters.AddWithValue("#Filename", [Filename])
...
command.Parameters.AddWithValue("#Discipline", Discipline)
command.Connection.Open()
Dim i As Int64 = command.ExecuteScalar()
command.Connection.Close()
'returns record ID'
Return i
Catch ex As Exception
Throw ex
Finally
If conn.State <> ConnectionState.Closed Then
conn.Close()
End If
End Try
Please note that, in this example, "myConnString" is a String with the value of my connection string for the database I'm connecting to.
I took the good advice of transitioning the script to an Entity Framework model. At first, the stored procedure did not return what I wanted (it only returned an integer value of 1), but I found a resource online that instructed me to create a "Function Import" in the EF model. After doing that and properly matching variable types I managed to get the stored procedure to return the right value in Visual Studio. Here's a good resoure:
http://www.devtoolshed.com/using-stored-procedures-entity-framework-scalar-return-values
Outside of converting from TableAdapter to Entity Framework (a good move regardless), you would be much_better off returning such a value as either an OUTPUT parameter or in a result set. A stored procedure return value is really intended to convey a status code of the operation and is hence pretty limited.
Also, by doing the following operation after the INSERT (i.e. as a separate step),
--Get the ID of this new file
SELECT #file_id = current_value FROM sys.sequences WHERE name = 'FILE_ID_SEQ';
you are risking getting an incorrect value if some other process inserts into that table at roughly the same time (i.e. between this particular INSERT and this particular SELECT).
Given both of the above issues, it would be fairly simple, and very reliable, to instead use the OUTPUT clause on the INSERT statement:
--Insert values for new file
INSERT INTO files.GENERAL
(FILENAME, EXTENSION, PATH_FULL, RELATIVE_PATH, JOB, DATE_MODIFIED, SIZE,
INDEX_MODULE, FILE_TYPE, DOC_TYPE, DISCIPLINE)
OUTPUT INSERTED.FILE_ID
VALUES(#FILENAME, #EXTENSION, #PATH_FULL, #RELATIVE_PATH, #JOB, #DATE_MODIFIED,
#SIZE, #INDEX_MODULE, #FILE_TYPE, #DOC_TYPE, #DISCIPLINE);
That will generate a 1 row, 1 column result set. The column will be named FILE_ID or whatever the actual field name is (I guess based on the naming convention used for the other fields and the variable).
Then get rid of the DECLARE #file_id, the SELECT ..., and the RETURN ....
I'm writing some flows for IBM WebSphere Message Broker which call stored procedures on a remote Microsoft SQL Server database. My problem is that I'm sometimes getting the resultset which should be returned and sometimes not getting anything.
The lines in the stored procedure which seem to be causing the trouble are:
IF (#noCountInd > 0)
SET NOCOUNT ON;
When I call the stored procedure from a database node in the Message Broker it returns a resultset on the first call then nothing on subsequent calls. If the SET NOCOUNT ON is unconditional it works every time. It works every time even with the above condition if called through the SQL Server Management Studio command line.
It also seems as if when enough time has passed between calls for the Message Broker to close its database connection then the next call with a new connection will succeed.
Here's my pared down code to produce this problem:
Stored procedure
CREATE PROCEDURE dbo.pTestConditionalNoCount
#noCountInd bit = 0
AS
IF (#noCountInd > 0)
SET NOCOUNT ON;
SELECT 'Success' AS RESULT;
RETURN 0;
ESQL in database node
CREATE DATABASE MODULE testConditionalNoCount
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
CALL pTestConditionalNoCount(TRUE,
Environment.Variables.testConditionalNoCount.Results.Row[])
IN Database.{'DATABASE_NAME'}.{'dbo'};
RETURN TRUE;
END;
END MODULE;
CREATE PROCEDURE pTestConditionalNoCount(IN testNoCountInd BOOLEAN)
LANGUAGE DATABASE
DYNAMIC RESULT SETS 1
EXTERNAL NAME pTestConditionalNoCount;
Output from trace node
Environment: ( ['MQROOT' : 0x29692478]
(0x01000000:Name):Variables = (
(0x01000000:Name):testConditionalNoCount = (
(0x01000000:Name):Results = (
(0x01000000:Name):Row = (
(0x03000000:NameValue):RESULT = 'Success' (CHARACTER)
)
)
)
)
)
Environment: ( ['MQROOT' : 0x29692478]
(0x01000000:Name):Variables = (
(0x01000000:Name):testConditionalNoCount = (
(0x01000000:Name):Results =
)
)
)
Environment: ( ['MQROOT' : 0x29692478]
(0x01000000:Name):Variables = (
(0x01000000:Name):testConditionalNoCount = (
(0x01000000:Name):Results =
)
)
)
The version of SQL Server is Microsoft SQL Server 2005 - 9.00.5324.00 (X64) and message broker is IBM WebSphere Message Broker 8.0.0.2.
Anyone have any idea what is going on here?
Update
I ran an ODBC trace and it shows two resultsets being returned in the failure case. The first is empty and the second is the resultset I'm expecting.
There seems to be no difference in the way the procedure is called. A SQLExecute is logged for each call then a SQLNumResultCols which returns 1 if it worked and 0 if it didn't.
I suspect that this is related to the caching of the stored procedure objects which is why when you let the connection idle out you see the call work again.
Might be worth examining an ODBC trace of 2 subsequent calls and see if there is any difference between the 2 executions (other than no results of course).