Hi,xxd
If you wanna get data from database without using dataset.
maybe you can try function.
By the below case, the SQL substring can't add the local varity into the sentance.
It was because the "exec sp_executesql " will be the other Transcation.
Hello xxd
as the requirement,I think you need to clac the value @total return for Client that call sp [MAXTIME]
this is the sample code I wrote, try it. modi by your sample code.
if you need the detail for this. Let me know. :)
why "cursor"
my target was get the return value From dynamic-SQLstring,
using the cursor delcare in global.
then fetch its content for out return value.
it's a better method.
Reference: Stored Procedure,Cursor;
Cheers,
Hunt
/* Sample Code by Hunt Begin*/
create proc [MaxTime]
(@number varchar(30),
@numbera varchar(30),
@numberb varchar(30),
@Total float output)
--here is the only modified i made
as
begin
declare @table varchar(20)
declare @freetotal varchar(20)
declare @SQL nvarchar(4000)
select @balance = balance, @table = table, @freetotal = freetotal
from info where number = @number
set @sql =
' Declare tmpcur cursor for '
' select @A = A FROM' + @table
+ ' WHERE LEFT(code, 1) = ' + LEFT(@incomingcode, 1)
+ ' AND CHARINDEX(LTRIM(RTRIM(code)), ' + @incomingcode+ ') = 1' +
' ORDER BY LEN(code) DESC'
exec (@sql)
open tmpcur;
Fetch Next From tmpcur into @A;
close tmpcur;
Deallocate tmpcur;
select @total = @balance / @A + @freetotal
/*Sample Code by Hunt End*/
ok,the important checkpoint on sys.procedure -> "exec sp_executesql"
check with my sample.
you'll get what you want.
Hint: as you set output varity with value,you shouldn't return any value. it's useless.
Reference with Books Online "sp_executesql"
Cheers,
Hunt
alter proc [sp_test1]
(@number varchar(30),
@numbera varchar(30),
@numberb varchar(30),
@Total float output,
@balance float output)
as
declare @table varchar(20)
declare @freetotal varchar(20)
declare @A float
declare @SQL nvarchar(4000);
declare @SQLparm nvarchar(500);
set @table ='car'
set @balance = 3.0
set @freetotal = 2.0
--the section below will be the most important.
set @SQLparm = N'@A float output'
select @sql = ' select @A = 2.0 FROM ' + @table
exec sp_executesql @sql,@SQLparm ,@A output
set @Total = @balance / @A + @freetotal
go
declare @tot float
declare @free float
exec [sp_test1] '1','2','3',@tot output,@free output
print '---'
print @tot
print @free
go
this thread got a little problem,i can't post any word on it.
try put default value behind the varity.
@total float =0 out,@balance float =0 out
in this sample,you can call sp with no output param.
check it.
Cheer.
You have to put the default value when you create stored procedure.
as below
Create Proc [MAXTime] (@numbera varchar(20),@numberb varchar(20),@numberc varchar(20),
@total float =0 out,@balance float =0 out);
after you alter the sp,you can call the proc by
exec [MAXTime] '1','2','3'
or
exec [MAXTime] '1','2','3',@total out,@balance out
try it.
I think I don't know what's your original requirement(or question).
maybe it could be describe more detail. On basiclly, the output Question seems like be sloved.
anyway,when we use stored procedure, it was defined for regular process.
and the return types that you said in last post were the normal method.
(I add the 4th as fire_trigger).
1. select @something
2. @something output
3. return@something and return(0)
4. sometimes we also set it up as another type of trigger.
Best Regrads. :)