calling stored procedure with linq - asp.net

I am new to Linq server.
I have a stored procedure in my databse that retuens count number.
select COUNT(*) from tbl_WorkerUsers
where WorkerCode=#Wcode
when I run it directly in my database it returns 1.
exec checkWorkerCodeAvailibility 100000312
but when I run it in c# code it always returns null.
WorkerDataContext Wkc = new WorkerDataContext();
int? result = Wkc.checkWorkerCodeAvailibility(Int32.Parse(Wcode)).Single().Column1;
what's wrong?

Define your Stored Procedure like this:
CREATE PROCEDURE [dbo].[checkWorkerCodeAvailibility]
#Wcode int = 0
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Result INT
SELECT #Result = COUNT(*) FROM tbl_WorkerUsers WHERE WorkerCode=#Wcode
RETURN #Result
END
You can then access this using the following code:
int result = db.checkWorkerCodeAvailibility(Int32.Parse(WCode));

Related

Recursive stored procedure in HSQLDB

is it possible to create recursive stored procedure in HSQLDB ?
I wrote the following one to update a record and recursively all the parent records:
CREATE PROCEDURE updateFolderTotals(IN p_id VARCHAR(32), IN p_size BIGINT, IN p_files INT, IN p_folders INT)
MODIFIES SQL DATA
BEGIN ATOMIC
DECLARE l_parentid VARCHAR(32);
UPDATE folders
SET tot_files = tot_files + p_files,
tot_size = tot_size + p_size ,
tot_folders = tot_folders + p_folders
WHERE id = p_id;
SELECT parentid INTO l_parentid FROM folders WHERE id = p_id;
IF (l_parentid IS NOT NULL) THEN
CALL updateFolderTotals(l_parentid,p_size,p_files,p_folders);
END IF;
END;
but I get the following error:
user lacks privilege or object not found: UPDATEFOLDERTOTALS / Error Code: -5501 / State: 42501
In HyperSQL User Guide I've found some info (see Recursive Routines in HyperSQL User Guide) but it seems it is supported for funtions only.
Thank you in advance for support.
You can create recursive procedures following the same guidelines. First create the procedure with a simple body that throws an exception. You need to specify the SPECIFIC name of the procedure:
CREATE PROCEDURE updateFolderTotals(IN p_id VARCHAR(32), IN p_size BIGINT, IN p_files INT, IN p_folders INT)
SPECIFIC updateFolderTotals_1 MODIFIES SQL DATA
SIGNAL SQLSTATE '45000'
Then alter the created procedure with the full body:
ALTER SPECIFIC ROUTINE updateFolderTotals_1
BEGIN ATOMIC
DECLARE l_parentid VARCHAR(32);
UPDATE folders
SET tot_files = tot_files + p_files,
tot_size = tot_size + p_size ,
tot_folders = tot_folders + p_folders
WHERE id = p_id;
SELECT parentid INTO l_parentid FROM folders WHERE id = p_id;
IF (l_parentid IS NOT NULL) THEN
CALL updateFolderTotals(l_parentid,p_size,p_files,p_folders);
END IF;
END;

how can i call stored procedure from the function in mariadb?

i want to get count of no.of rows present in table which i pass at runtime to a function.
i have created a procedure and function to execute dynamic queries. function will not allow dynamic query because i am calling procedure from function.
that procedure having dynamic query.
///////procedure///////
CREATE PROCEDURE bizopsgolddev.`test1`(tbnm varchar(100))
begin
declare sql_text varchar(200);
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SELECT CONCAT(sql_text, ' is not valid');
END;
set sql_text=concat('select count(*) from ',tbnm);
SET #SQL := sql_text;
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
end;
//////function//////
DROP FUNCTION IF EXISTS xyz;
CREATE FUNCTION `xyz`(tname varchar(100)) RETURNS int(11)
begin
declare val int;
call test1(tname);
return 1;
end;
if i execute this //select xyz('axpdc')// it should return rows count
can any one tell me how can i get count by passing table name to function(in mariadb only)
As I understand the question, the solution would be a function that returns the row count of a table with it's name passed to the function as a parameter.
I think this could be done by querying the information_schema database in MariaDB. A function could look like this:
CREATE DEFINER = 'yourUsername'#'192.168.%'
FUNCTION testDataBase.fn_GetRowCount(tableName VARCHAR(128))
RETURNS int(11)
BEGIN
-- This could be a parameter if need it to be.
DECLARE databaseName varchar(40) DEFAULT 'testDataBase';
DECLARE result int DEFAULT -1;
SELECT t.TABLE_ROWS INTO result
FROM information_schema.TABLES t
WHERE t.TABLE_NAME = tableName
AND t.TABLE_SCHEMA = databaseName;
RETURN result;
END
In order for this to work the user mentioned as the definer must have read privilege to the TABLES table in the information_schema database, otherwise you might get an error (tbh, I don't know if this is necessary).
There is a lot of useful information to be grabbed from the information_schema database.

Sybase IQ, return the result of a query string in a variable inside a function with "execute"?

Hi can you help me please?, That way I can return the result of a query string in a variable inside a function with "execute"?.
for example
create function my _funcion(in # num_celular char (20), in # str_comando char (120))
returns char (120)
- on exception resume
as
begin
set # exec_qry = Select 1 from dummy
execute (# exec_qry)
return (# the return value of the query "execute")
end
go
thanks
Try this way:
create function my_funcion(#num_celular char (20), #str_comando char (120))
returns char(120)
as
begin
DECLARE #Result char(120)
execute('select #Result = ''Aaa'' ' )
RETURN #Result
end
go
Result of below query
select my_funcion('AA','BB')
is
Aaa
Ok, thanks for answering my question, but look at the issue is much more complex because we are running a Dynamic Sql, formed in while loop to the query is then stored in one string variables and running it, the problem are the function promptly made ​​as follows and is attached below.
create function ms_trafico.fn_mov_cambia_tag( in #num_celular char(20), in #str_comando char(120) )
returns char(120)
--on exception resume
as
begin
set nocount on
set temporary option string_rtruncation='OFF'
--aplican para isql
--set option isql_print_result_set=ALL
--set option isql_show_multiple_result_sets=On
declare #comando varchar(100)
declare #exec_qry varchar(500)
declare #cadena varchar(60)
declare #tag char(15)
declare #columna char(20)
declare #pos int
declare #strCadFinal char(150)
declare #strFono char(20)
set #comando = trim(#str_comando)
set #strFono = trim(#num_celular)
set #exec_qry='select XXX '
set #pos = 1
print "comando " + #comando
while(#pos <= locate(#comando,'<',-1))
begin
set #tag = substr(#comando,locate(#comando,'<',#pos),CHARINDEX('>', SUBSTR(#comando,locate(#comando,'<',#pos),length(#comando))))
set #pos = locate(#comando,'<',#pos) + 1
select atributo_campo into #columna from TB_MOV_MAPEO_COMANDOS where parametro = #tag
set #cadena="replace(XXX,'"||trim(#tag)||"',"||trim(#columna)||")"
set #exec_qry=replace(#exec_qry,'XXX',#cadena)
end
set #comando="'"||trim(#comando)||"'"
set #exec_qry=replace(#exec_qry,'XXX',#comando)
set #exec_qry=#exec_qry||' as comando INTO #strCadFinal FROM VM_ALL_SERVICES_MOVIL where num_celular=#strFono'
execute (#exec_qry)
return (#strCadFinal) "The result of my query with execute method"!!!!!!!!!!
end
go
Thanks for you attention.

Entity Framework shows error when called stored procedure

In my project EF calls a stored procedure which is shown below. It returns either 1 or scope identity.
On EF function imports, the stored procedure is listed with a return type of decimal.
When the stored procedure returns scope identity, everything is ok.
But when if condition of sp satisfies, ef throws error as
The data reader returned by the store data provider does not have enough columns for the query requested.
Pls help..
This is my stored procedure:
#VendorId int,
#ueeareaCode varchar(3),
#TuPrfxNo varchar(3),
#jeeSfxNo varchar(4),
#Tjode varchar(3),
#uxNo varchar(3),
#TyufxNo varchar(4),
#Iyuy bit
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
IF EXISTS (Select dfen_id
from dbo.efe_phfedwn_eflwn
where
[yu] = #Tyuode and
[uy] = #TuyxNo and
[yuno] = #Tuo)
return 1
ELSE
Begin
INSERT INTO dbo.yu
....................
Select Scope_Identity()
End
END
The error tells us that EF is expecting a result set and when we use RETURN we don't get a result set. Your error means that the stored procedure is returning an integer but EF is expecting a decimal, so we just CAST the selected values to a decimal.
So modify the SQL so that we SELECT instead of RETURN, like so (not forgetting to use CAST):
IF EXISTS (Select cntct_ctr_phn_ln_id
from dbo.cntct_ctr_phn_ln
where
[toll_free_phn_area_cd] = #TollfreeareaCode and
[toll_free_phn_prfx_no] = #TollfreePrfxNo and
[toll_free_phn_sfx_no] = #TollfreeSfxNo)
SELECT CAST(1 AS decimal)
Then also CAST the result of SCOPE_IDENTITY() to a decimal:
SELECT CAST(SCOPE_IDENTITY() AS decimal)

How to have a MySQL procedure return a boolean?

I want to create a procedure that takes in a string, searches the table and the specified column for that string, and returns a 1 if it finds it and a zero if it does not. I am relatively new to SQL and do not know the syntax or commands very well. I want something like this:
CREATE PROCEDURE dbo.GetUsername
(
#Username NCHAR(10)
)
AS
#boolVariable
SELECT #Username FROM Accounts
RETURN #boolVariable
You don't return a value, but instead supply that in a result set.
CREATE PROCEDURE GetUsername
(
#Username NCHAR(10)
)
AS
SELECT Username FROM Accounts WHERE Username = #UserName;
In your calling code, simply check for the existence of a row in the result set.
I'm not sure if you're looking for mysql or mssql solution.
delimiter //
drop procedure if exists search_string //
create procedure search_string (in str varchar(100))
begin
declare b,r bool;
select count(*) into r from your_table where your_field = str;
if r > 0 then
set b = 1;
else
set b = 0;
end if;
select b;
end; //
delimiter //
call search_string('searched_string');

Resources