Technical Article

VB.Net support from your SQLServer DBA

,

Once devs are pushed toward using parameters, they get confused as to why their sp is not reused and has many compilations when in use.
The reason is most of the time, that the parameters are defined with a wrong datatypemapping in the application or have different datatypes in a # of applications.

I use this proc the generate the .Net-code to define and use the parameters for a commandobject named sqlcmd. A simple copy/paste helps out in this case.

It is primitive, and it may have some flaws, but it helps out in 90 % ...

Create  proc [dbo].[spc_ALZDBA_CreateSqlParameter] 
  @ProcedureName sysname 
, @ProcedureSchema sysname = NULL
as 
begin
-- When used in SQL2000 : @ProcedureSchema is the Owner of the procedure.

set nocount on
if isnull(@ProcedureSchema, '')  = ''
   begin
If exists (select top 1 1 from INFORMATION_SCHEMA.PARAMETERS where SPECIFIC_NAME = @ProcedureName and SPECIFIC_SCHEMA= User_Name() )
  begin
set @ProcedureSchema = User_Name() 
  end
else
  begin
set @ProcedureSchema = 'dbo'
  end

  end

Select cast('        wrkCmdParam = New SqlClient.SqlParameter  
        With wrkCmdParam 
            .ParameterName = "' + Parameter_Name + '" 
            .SqlDbType = SqlDbType.' + Data_Type  + '
            ''.DbType = DbType.String 
            ' + isnull('.Size = ' + cast(Character_Maximum_Length as varchar(15)) , '''.Size = ') + '
            .Direction = ParameterDirection.' + PARAMETER_MODE_dotNet + '
        End With 
        sqlcmd.Parameters.Add(wrkCmdParam) 
 ' as varchar(4000))

FROM (
    SELECT ORDINAL_POSITION, PARAMETER_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
, CASE PARAMETER_MODE WHEN 'IN' THEN 'Input' 
WHEN 'OUT' THEN 'Output' 
WHEN 'INOUT' THEN 'InputOutput' 
ELSE '???' + PARAMETER_MODE
END as PARAMETER_MODE_dotNet
    FROM INFORMATION_SCHEMA.PARAMETERS
    WHERE SPECIFIC_SCHEMA = @ProcedureSchema
        AND SPECIFIC_NAME = @ProcedureName
    UNION all
    SELECT 0, '@RETURN_VALUE', 'int', NULL, 'ReturnValue'
) x 

order by ORDINAL_POSITION

print '---'

select 'sqlcmd.parameters("' + Parameter_Name + '").value = rij.Item("' + substring(Parameter_Name, 2, 255) + '")'
FROM INFORMATION_SCHEMA.PARAMETERS
where SPECIFIC_NAME = @ProcedureName
  and SPECIFIC_SCHEMA = @ProcedureSchema 
order by Ordinal_Position



end


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


GRANT  EXECUTE  ON [dbo].[spc_ALZDBA_CreateSqlParameter]  TO [YourDevelopersGroup]
GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating