Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ducletan
Starting Member
25 Posts |
Posted - 2010-12-09 : 03:20:07
|
| Help me:create procedure sp1@giatri bit,@output nvarchar(max) OUTPUTasbeginif @giatriset @output='OK 'elseset @output='Failse 'endcreate Function F1@giatri bit,returns nvarchar(max)asbegindeclare @output nvarchar(max)if @giatriset @output='OK 'elseset @output='Failse 'return @outputendIn VB.NETsp1 return: 'O' or 'F' (first char)F1 return 'OK ' or 'Failse 'Thanks |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-12-09 : 04:01:59
|
quote: Originally posted by ducletanIn VB.NETsp1 return: 'O' or 'F' (first char)F1 return 'OK ' or 'Failse 'Thanks
I feel its not the problem with SP code but may be the way you are capturing the output returned by SP.You might have declared the variable and not specified its length.Can you show us the code used for calling the SP |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-12-09 : 04:07:14
|
First of all there are syntax errors in the function and the procedure:create procedure sp1 @giatri bit, @output nvarchar(max) OUTPUTasbeginif @giatri = 1 set @output='OK 'else set @output='Failse 'endcreate Function F1 ( @giatri bit)returns nvarchar(max)asbegindeclare @output nvarchar(max)if @giatri = 1 set @output='OK 'else set @output='Failse 'return @outputend Then the following code will give you exactly the same result:select dbo.F1(0)declare @out varchar(max)exec sp1 0, @out outputselect @out In other words: you have probably declared the output variable in your VB.NET code too narrow.- LumbagoMy blog (yes, I have a blog now! just not that much content yet) -> www.thefirstsql.com |
 |
|
|
ducletan
Starting Member
25 Posts |
Posted - 2010-12-09 : 04:48:10
|
| create procedure sp1 @giatri bit, @output nvarchar(max) OUTPUTasbeginif @giatri = 1 set @output='OK 'else set @output='Failse 'endcreate Function F1 ( @giatri bit)returns nvarchar(max)asbegindeclare @output nvarchar(max)if @giatri = 1 set @output='OK 'else set @output='Failse 'return @outputendIN VB.NETdim colecc as new collectioncolecc.add(new Data.SQLClient.sqlParameter("@giatri",0) 'or 1dim str as stringstr=execFunction(SQL_connection,"F1",colecc) return 'OK ' or 'Failse 'dim colecc as new collectioncolecc.add(new Data.SQLClient.sqlParameter("@giatri",0) 'or 1dim str as stringstr=execProc(SQL_connection,"SP",colecc) return 'O' or 'F' Public Function execFunction(byref SQL_connection as sqlconnection,ByVal funcName As String, Optional ByVal params As Collection = Nothing) As String Dim cmd As SqlCommand Dim param As SqlParameter Dim sCmdText As String cmd = New SqlCommand cmd.CommandType = CommandType.StoredProcedure cmd.Connection = SQL_connection cmd.CommandText = funcName cmd.CommandTimeout = 0 If Not params Is Nothing Then For iParam = 1 To params.Count cmd.Parameters.Add(New Data.SqlClient.SqlParameter(params(iParam).ToString, params(iParam).value)) Next End If cmd.Parameters.Add(New Data.SqlClient.SqlParameter("@OutPut", Space(100))) cmd.Parameters("@OutPut").Direction = ParameterDirection.ReturnValue cmd.ExecuteNonQuery() Return cmd.Parameters("@OutPut").Value.ToString.Trim End Function Public Function execProc(byref sql_ByVal funcName As String, Optional ByVal params As Collection = Nothing) As String Dim cmd As SqlCommand Dim param As SqlParameter Dim sCmdText As String cmd = New SqlCommand cmd.CommandType = CommandType.StoredProcedure cmd.Connection = SQL_connection cmd.CommandText = funcName cmd.CommandTimeout = 0 If Not params Is Nothing Then For iParam = 1 To params.Count cmd.Parameters.Add(New Data.SqlClient.SqlParameter(params(iParam).ToString, params(iParam).value)) Next End If cmd.Parameters.Add(New Data.SqlClient.SqlParameter("@OutPut", Space(100))) cmd.Parameters("@OutPut").Direction = ParameterDirection.Output cmd.ExecuteNonQuery() Return cmd.Parameters("@OutPut").Value.ToString.Trim End Function |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-12-09 : 05:22:12
|
| Would try changing the nvarchar(max) to nvarchar(10) and varchar(10)Also check what sqldbtype the parameter has been created as. I always set the type.Would be easier to test with inline code rather than calling execproc.Also the execproc definition doesn't seem to match the call - maybe you aren't calling the function you think you are.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|
|