Struggling with stored procedures and MSSQL #1846
Unanswered
PeterNierop
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I live in the world of C, flirt openly with TSQL and have probably written several thousand stored procedures, some multiple pages long.
I am however an absolute noob with moist puppy eyes on Rust. As in, I begin to enjoy the language, yet daily have to show blue and black for the ultimate fighting machine: the compiler....
It so happens that I am tasked to generate a server for hundreds of people who need to sometimes talk to MSSQL. Hence, I would like to use prepared stored procedures with both input and output parameters. And it seems Tokio is a decent async platform for that.
So, I be happy to donate to the first person two hundred dollars on PayPal for this:
A working sample that creates an MSSQL pool, creates some SQL statement (MSSQLStatement??) that I can reuse repeatedly by just changing the input values and catching the output values. These are either tinyint, smallints, bigints or utf8 varchar. It would be nice to see handling of the return code as well.
I did notice that sqlx seems to have i8 instead of u8 handling for tinyint... not sure how to fix that other than making it all smallints.
This might help start the SQL stuff:
if object_id('dbo.test') is not null drop proc dbo.test
go
create proc dbo.test
@ti tinyint
, @to tinyint = null output
, @si smallint
, @so smallint = null output
, @vi varchar(20)
, @vo varchar(20) = null output
with encryption
as
set nocount on;
if @ti is null return 1;
if @si is null return 2;
if @vi is null or len(@vi)=0 return 3;
-- set output values
select @to = cast(250 as tinyint) -- this should be positive in Rust.
, @so = @si + 5
, @vo = 'Maybe'; -- preferable this should be carried into rust as varchar, not nvarchar...
-- let us also select some row/columns from a quick table
declare @TBL as table (b bigint primary key, v varchar(10));
insert into @TBL values (4,'This'), (5,'might'),(6,'Work');
select * from @TBL;
return 10;
go
grant exec on dbo.test to public;
declare @rc int, @ti tinyint = 1, @to tinyint, @si smallint = 2, @so smallint, @vi varchar(20) = 'Does this work?', @vo varchar(20);
exec @rc = dbo.test @ti, @to output, @si, @so output, @vi, @vo output;
select @rc, @to, @so, @vo;
Thank you in advance.
Beta Was this translation helpful? Give feedback.
All reactions