AlwaysEncrypted Column error when trying to insert into table - stored-procedures

lets start with the error first:
Encryption scheme mismatch for columns/variables . The encryption scheme for the columns/variables is (encryption_type = 'PLAINTEXT') and the expression near line '3' expects it to be (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'CEK_P_H', column_encryption_key_database_name = 'Payments_Hub') (or weaker)
I'm trying to insert into a column with AlwaysEncrypt encryption on using a stored procedure:
The column 'PARAMETER_VALUE' is the encrypted column
CREATE PROCEDURE dbo.SP_UI_Parameter
(
#ID int OUTPUT,
#OPERATION_ID int,
#PARAMETER_NAME varchar(100),
#PARAMETER_METRIC varchar(100),
#PARAMETER_VALUE varchar(100)
)
AS
BEGIN
IF #ID IS NULL
BEGIN
INSERT INTO [dbo].[TOKEN_PARAMETER] (OPERATION_ID,
PARAMETER_NAME, PARAMETER_METRIC, PARAMETER_VALUE)
SELECT #OPERATION_ID, #PARAMETER_NAME, #PARAMETER_NAME, #PARAMETER_VALUE
SET #ID = SCOPE_IDENTITY()
END
ELSE
BEGIN
UPDATE [dbo].[TOKEN_PARAMETER] SET
OPERATION_ID = #OPERATION_ID,
PARAMETER_NAME = #PARAMETER_NAME,
PARAMETER_METRIC = #PARAMETER_METRIC,
PARAMETER_VALUE = #PARAMETER_VALUE
END
END
GO
Why am I getting the error for the query ?
Is there something I must include within the STORED PROCEDURE ?
I have included "Column Encryption Setting=Enabled " in my connection settings
I have also enabled Parameteterization for AlwaysEncrypted
The key is stored on the local machine
I have created DETERMINISTIC encryption
SQL Server 2016 database Engine using SSMS 17 - can this be an issue ?
Thanks In Advance

Related

MariaDB Insert Stored Procedure With Parameter

I migrated my company database from MySQL to another hosting firm not knowing the hosting firm uses MariaDB, upon try to create my stored procedure with my IN parameter, MariaDB is seeing the parameter as a columnn. See the stored procedure code below with the error :
CREATE PROCEDURE ADD_WITHDRAWAL_A(IN withdrawalcode_p VARCHAR(25), IN id_p VARCHAR(8), IN amount_p VARCHAR(12), IN datewithdrawn_p VARCHAR(35), IN approved_p VARCHAR(8))
START TRANSACTION;
INSERT INTO Withdrawals(WithdrawalCode, IDD, Amount, DateWithdrawn, Approved)
VALUES (withdrawalcode_p, id_p, amount_p, datewithdrawn_p, approved_p);
UPDATE account SET AccountBalance = AccountBalance - amount_p WHERE IDD = id_p LIMIT 1;
COMMIT;
***** AFTER RUNNING THE ABOVE CODE, MARIADB GAVE THIS ERROR :
Error
SQL query:
INSERT INTO WithdrawalRequest( WithdrawalCode, IDD, Amount, DateWithdrawn, Approved )
VALUES (withdrawalcode_p, id_p, amount_p, datewithdrawn_p, approved_p);
MySQL said: Documentation
#1054 - Unknown column 'withdrawalcode_p' in 'field list'
The column name is WithdrawalCode and not 'withdrawalcode_p', 'withdrawalcode_p' is a parameter passed in to the stored procedure but the server is seeing it as a field name. I spoke with the hosting firm and they said their database is MariaDB and not MySQL. This same code worked in MySQL server.
I will appreciate any useful help rendered here.
You forgot to set non-default delimiters and to wrap the procedure body into BEGIN/END, which is necessary since it has more than one statement.
What happens in your case is that the procedure created with body START TRANSACTION, and the rest is considered to be a set of ordinary statements. Instead, try
DELIMITER $$
CREATE PROCEDURE ADD_WITHDRAWAL_A(IN withdrawalcode_p VARCHAR(25), IN id_p VARCHAR(8), IN amount_p VARCHAR(12), IN datewithdrawn_p VARCHAR(35), IN approved_p VARCHAR(8))
BEGIN
START TRANSACTION;
INSERT INTO Withdrawals(WithdrawalCode, IDD, Amount, DateWithdrawn, Approved)
VALUES (withdrawalcode_p, id_p, amount_p, datewithdrawn_p, approved_p);
UPDATE account SET AccountBalance = AccountBalance - amount_p WHERE IDD = id_p LIMIT 1;
COMMIT;
END $$
DELIMITER ;

DB2 error : Keyword SELECT not expected. Valid tokens: FOR WITH WITHOUT

I am trying to execute the following stored procedure on db2 database using iSeries Navigator:
CREATE PROCEDURE ZSPPQDELETEUSERIDBA (
IN P_USERID CHAR(10) ,
IN P_BUSINESSAREA CHAR(10))
DYNAMIC RESULT SETS 1
LANGUAGE SQL
SPECIFIC ZSPPQDELETEUSERIDBA
NOT DETERMINISTIC
MODIFIES SQL DATA
CALLED ON NULL INPUT
SET OPTION ALWBLK = *ALLREAD ,
ALWCPYDTA = *OPTIMIZE ,
COMMIT = *CS ,
CLOSQLCSR = *ENDMOD ,
DECRESULT = (31, 31, 00) ,
DFTRDBCOL = *NONE ,
DYNDFTCOL = *NO ,
DYNUSRPRF = *USER ,
RDBCNNMTH = *RUW ,
SRTSEQ = *HEX
P1 : BEGIN
DECLARE TOTALCNT INTEGER DEFAULT 0 ;
DECLARE C1 CURSOR WITH RETURN TO CALLER
SELECT Count (*) FROM USERBUSINESSAREA
WHERE USERID = P_USERID
AND BUSINESSAREA = P_BUSINESSAREA;
OPEN C1;
FETCH C1 INTO TOTALCNT;
IF ( TOTALCNT = 1) THEN
DELETE
FROM USERBUSINESSAREA
WHERE USERID = P_USERID
AND BUSINESSAREA = P_BUSINESSAREA;
END IF;
END P1 ;
I am getting the following error when I try to execute the above procedure:
SQL State: 42601
Vendor Code: -199
Message: [SQL0199] Keyword SELECT not expected. Valid tokens: FOR WITH WITHOUT. Cause . . . . . : The keyword SELECT was not expected here. A syntax error was detected at keyword SELECT. The partial list of valid tokens is FOR WITH WITHOUT. This list assumes that the statement is correct up to the unexpected keyword. The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery . . . : Examine the SQL statement in the area of the specified keyword. A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.
What's going wrong here?
I found out where the problem is.I was supplying the wrong input values for the stored procedure(i.e one in place of another..data types are different). That caused the issue. Now it is resolved

What is the type (none,scaler,entity,complex) if store procedure return multiple ouptput parameter in entity framework in mvc4

I have created a mvc4 application with entity framework. Added a entity model in project. Now i have
added a store procedure in model browser and editing import function. There is a option Returns a collection of which contains none,scalers,complex,entities. I am not able to decide which one to choose as my store procedure returns multiple output parameters. If it returns single parameter then i can choose scalers, if table then entities. But it returns more then one output parameter so which one to choose. I am attaching store procedure screen shot.
Your stored procedure uses reference parameters, but doesn't actually return anything. To make a stored procedure return something, end the procedure with a SELECT statement that doesn't set a variable.
So, your code with look something like this:
CREATE PROC [wickedbrains].[uspValidateAdminLogin]
#UserName VARCHAR(50),
#Password VARCHAR(50)
AS
BEGIN
DECLARE #UserId INT = NULL,
#Res INT = 0;
IF EXISTS(SELECT '' FROM tblAdminUser WHERE UserName = #UserName AND Pwd = #Password)
BEGIN
SELECT #UserId = Id FROM tblAdminUser WHERE UserName = #UserName AND Pwd = #Password;
SET #Res = 1;
END
SELECT #UserId, #Res;
END
Once you've fixed your stored procedure, as Ehsan described, you can fix your imported stored procedure after the fact by clicking Get Column Information, then clicking Create New Complex Type.
If you absolutely have to use output parameters, you will have to retrieve the parameters with code as you would with reference parameters used in any other function. The point is that stored procedures that only use output parameters don't have a return type. See this answer for further details: https://stackoverflow.com/a/6193419/12116036

SSIS Script Component Call Stored Procedure returns -1

I tried implementing a call to Stored proc and the proc returns ID which will used later.
Everytime I execute I get the out parameter as -1. Below is my sample code:
OleDbCommand sqlStrProc = new OleDbCommand();
sqlStrProc.Connection = dbConn;
sqlStrProc.CommandText = "dbo.insert_test";
sqlStrProc.CommandType = CommandType.StoredProcedure;
sqlStrProc.Parameters.Add("#p_TestID", OleDbType.Integer, 255).Direction = ParameterDirection.Output;
sqlStrProc.Parameters.Add("#p_TestName", OleDbType.VarChar).Value = "Test";
sqlStrProc.Parameters.Add("#p_CreatedBy", OleDbType.VarChar).Value = "Test";
int personID = sqlStrProc.ExecuteNonQuery();
Row.outPersonID = personID;
personID is always -1. What am I doing wrong here. Please help..!!
Below is the stored proc code
CREATE PROCEDURE [dbo].[INSERT_TEST]
#p_TestID int OUTPUT,
#p_TestName varchar (50),
#p_CreatedBy varchar (100)
AS
SET NOCOUNT ON
INSERT INTO Test(
TestName,
CreatedBy)
VALUES
( #p_TestName,
#p_CreatedBy)
SELECT #p_TestID = SCOPE_IDENTITY()
-1 could mean that the stored procedure failed to execute as desired and the transaction was rolled back. You may want to look for any truncation issues since you have different sizes for the 2 input parameters but are using the same input. Also I assume you have proper code to open and close connections etc?
-1 returned value is an error produced during the execution of your SP, this is due to the following reasons:
SP Structure: everytime you are executing the SP it tries to create it again while it already exists. so you have to either make it an ALTER PROCEDURE instead of CREATE PROCEDURE or do the following:
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[INSERT_TEST]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[INSERT_TEST]
GO
CREATE PROCEDURE [dbo].[INSERT_TEST]
#p_TestID int OUTPUT,
#p_TestName varchar (50),
#p_CreatedBy varchar (100)
AS
Database Connection (Table Name and Location): you have to specify withe the OLEDB the ConnectionString that connects you to the write DB. try to test the full Table path; like the following;
INSERT INTO [DATABASENAME].[SHCEMA].[TABELNAME](
Name,
CreatedBy)
VALUES
( #p_TestName,
#p_CreatedBy)
Define your SP as :
CREATE PROCEDURE [NAME]
AS
BEGIN
END
thought it is not a problem, but it is a proper way to write your SPs in terms of connection transactions,
Let me know if it works fine with you :)
Regrads,
S.ANDOURA

Sybase ASE - INSERT INTO statment i stored procedure, problems in formating string

I have following problem formatting the #p_f_field variable correct, I get the error:
Could not execute statement.
Incorrect syntax near ‘+’
Sybase error code=102, SQLState=”42000”
Severity Level=15, State=181, Transaction State=1
Line 7
My stored procedure:
create proc dbo.sp_m_efi_dw_full_dsa_table_load_im ( -- parameter examples:
#p_skm_name varchar(4096),
#p_usr_name varchar(4096),
#p_t_name_dsa varchar(4096),
#p_t_name_bas varchar(4096),
#p_f_name varchar(4096),
#p_f_field varchar(4096)
)
as begin
declare #sql varchar(4096)
if #p_skm_name is not null
begin
if #p_usr_name is not null
begin
set #sql='TRUNCATE TABLE '+#p_skm_name+'.'+#p_usr_name+'.'+#p_t_name_dsa
exec (#sql)
end
end
set #sql='INSERT INTO '+#p_skm_name+'.'+#p_usr_name+'.'+#p_t_name_dsa+' '+#p_f_name+
' VALUES '+#p_f_field
exec (#sql)
end
My call to the stored procedure:
execute BAS_efi.dbo.sp_m_efi_dw_full_dsa_table_load_im
#p_skm_name = 'B_ef',
#p_usr_name = 'dbo',
#p_t_name_dsa = 'a_log',
#p_t_name_bas = 'a_log',
#p_f_name = '(NEWFIELD1)',
#p_f_field = '('+char(39)+'daf9af01-6bc2-11e-b23182b0623e'+char(39)+')'
Any suggestions on how to format the variable #p_f_field correct, or any others suggestions on how to execute this simple INSERT INTO procedure?

Resources