Showing posts with label alterprocedure. Show all posts
Showing posts with label alterprocedure. Show all posts

Tuesday, March 27, 2012

Calling stored proc with default params from .NET not working.

All,

I have the following :

ALTERPROCEDURE [dbo].[sp_FindNameJon]

@.NameNamevarchar(50)='',

@.NameAddressvarchar(50)='',

@.NameCityvarchar(50)='',

@.NameStatevarchar(2)='',

@.NameZipvarchar(15)='',

@.NamePhonevarchar(25)='',

@.NameTypeIdint=0,

@.BureauIdint,

@.Pageint=1,

@.Countint=100000

AS

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SETNOCOUNTON;

DECLARE @.SqlStringnvarchar(3000),@.SelectClausenvarchar(1000), @.FromClausenvarchar(1000),@.WhereClausenvarchar(1000)

DECLARE @.ParentSqlStringnvarchar(4000)

DECLARE @.Startint, @.Endint

INSERTinto aaJonTempvalues(@.Page,'here2', @.NameCity);

And inside of aaJonTemp, I have the following :

NULLhere2NULLNULLhere2NULL

How is this possible? If @.Page or @.NameCity is NULL, how come it doesn't default to a value in the stored proc?

Thx

jonpfl

Because a parameter value being NULL is not the same thing as not supplying a parameter at all.

The defaults basically say... If the user hasn't supplied the parameter, then use this. You have supplied a parameter, although the value is NULL. If you want to make it so that if someone sends in a NULL and you want to change it to something else, then you need to code that.

IF @.param IS NULL SET @.param=...

sql

Wednesday, March 7, 2012

call function inside Stored Procedure Error

i call the function inside the stored procedure and when i execute it theres a error

ALTERPROCEDURE [dbo].[usp_LoadEmployees]

-- Add the parameters for the stored procedure here

AS

BEGIN

SELECT dbo.Employees.EmployeeID,dbo.fn_ProperEmployees(dbo.Employees.EmployeeID)as MyEmployee,

dbo.Employees.Age, dbo.Employees.Sex, dbo.Employees.PositionID, dbo.Positions.Position_Name, dbo.Employees.DepartmentID,

dbo.Departments.DepartmentName

FROM dbo.Employees INNERJOIN

dbo.Departments ON dbo.Employees.DepartmentID = dbo.Departments.DepartmentID INNERJOIN

dbo.Positions ON dbo.Employees.PositionID = dbo.Positions.PositionID

GROUPBY dbo.Employees.EmployeeID, dbo.Employees.MI,

dbo.Employees.Age, dbo.Employees.Sex, dbo.Employees.PositionID, dbo.Positions.Position_Name, dbo.Employees.DepartmentID,

dbo.Departments.DepartmentName

ORDERBY dbo.Employees.EmployeeID

END

- function is this -

ALTER FUNCTION [dbo].[fn_ProperEmployees](@.cEmployeeID varchar(50))

RETURNS TABLE

AS

RETURN

(

-- Add the SELECT statement with parameter references here

SELECT EmployeeName =(Upper(dbo.Employees.Lastname)+','+ dbo.Employees.Firstname +' '+ dbo.Employees.MI)

FROM Employees

WHERE EmployeeID = @.cEmployeeID

)

the error is

Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fn_ProperEmployees", or the name is ambiguous.

The function is a inline table function, if it is table function then you can't use it as column (only scalar functions are allowed)..

Change your function as follow as ,

Code Snippet

ALTER FUNCTION [dbo].[fn_ProperEmployees](@.cEmployeeID varchar(50))

RETURNS Varchar(1000)

AS

Begin

Declare @.EmployeeName as Varchar(1000);

-- Add the SELECT statement with parameter references here

SELECT

@.EmployeeName = (Upper(dbo.Employees.Lastname)

+ ',' + dbo.Employees.Firstname

+ ' ' + dbo.Employees.MI)

FROM

Employees

WHERE

EmployeeID = @.cEmployeeID;

Return @.EmployeeName;

End