PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.sqlserver.server > default value for int type in stored procedure
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
default value for int type in stored procedure

Réponse
 
LinkBack Outils de la discussion
Vieux 02/09/2008, 20h33   #1
AkAlan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut default value for int type in stored procedure

I'm trying to assign a default value to an int type column in a stored
procedure that will display all records if the user does not select an item
from a drop down list. I have tried ">0" and "%" but I get an error. How do
I select all records. Thanks for any .
  Réponse avec citation
Vieux 02/09/2008, 20h50   #2
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: default value for int type in stored procedure

Here is one way:

CREATE PROCEDURE Foo
@selected_item INT = 0
AS
SELECT <columns>
FROM Items
WHERE item_id = @selected_item
OR @selected_item = 0;

--
Plamen Ratchev
http://www.SQLStudio.com
  Réponse avec citation
Vieux 02/09/2008, 21h02   #3
AkAlan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: default value for int type in stored procedure

The ddl would return a valid ID if the user makes a selection or if nothing
is selected from the ddl I want the sp to reurn all records not those with
null or "0".

"John Bell" wrote:

>
> "AkAlan" <AkAlan@discussions.microsoft.com> wrote in message
> news:82B26BE7-9AD7-41B0-A3EA-61B27EABB71A@microsoft.com...
> > I'm trying to assign a default value to an int type column in a stored
> > procedure that will display all records if the user does not select an
> > item
> > from a drop down list. I have tried ">0" and "%" but I get an error. How
> > do
> > I select all records. Thanks for any .

>
> Hi
>
> You would need to post your DDL and expected results to make sure that
> people understand what you want.
>
> If you have a parameter to the stored procedure that is optional, the value
> of that parameter is probably NULL
>
> e.g.
>
> CREATE PROCEDURE usp_GetEmployee @empid int = NULL
> AS
> SELECT [EmployeeID]
> ,[NationalIDNumber]
> ,[ContactID]
> ,[LoginID]
> ,[ManagerID]
> ,[Title]
> ,[BirthDate]
> ,[MaritalStatus]
> ,[Gender]
> ,[HireDate]
> ,[SalariedFlag]
> ,[VacationHours]
> ,[SickLeaveHours]
> ,[CurrentFlag]
> ,[rowguid]
> ,[ModifiedDate]
> FROM [AdventureWorks].[HumanResources].[Employee]
> WHERE [EmployeeID] = @empid OR @empid IS NULL
>
>
> EXEC usp_GetEmployee 1
>
> EXEC usp_GetEmployee NULL
>
> EXEC usp_GetEmployee
>
> John
>

  Réponse avec citation
Vieux 02/09/2008, 23h04   #4
AkAlan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: default value for int type in stored procedure

Here is the solution I was looking for.

create procedure up_GetData @Id int =null
as
select * from MyTable
where id = isnull(@Id,id)




"AkAlan" wrote:

> The ddl would return a valid ID if the user makes a selection or if nothing
> is selected from the ddl I want the sp to reurn all records not those with
> null or "0".
>
> "John Bell" wrote:
>
> >
> > "AkAlan" <AkAlan@discussions.microsoft.com> wrote in message
> > news:82B26BE7-9AD7-41B0-A3EA-61B27EABB71A@microsoft.com...
> > > I'm trying to assign a default value to an int type column in a stored
> > > procedure that will display all records if the user does not select an
> > > item
> > > from a drop down list. I have tried ">0" and "%" but I get an error. How
> > > do
> > > I select all records. Thanks for any .

> >
> > Hi
> >
> > You would need to post your DDL and expected results to make sure that
> > people understand what you want.
> >
> > If you have a parameter to the stored procedure that is optional, the value
> > of that parameter is probably NULL
> >
> > e.g.
> >
> > CREATE PROCEDURE usp_GetEmployee @empid int = NULL
> > AS
> > SELECT [EmployeeID]
> > ,[NationalIDNumber]
> > ,[ContactID]
> > ,[LoginID]
> > ,[ManagerID]
> > ,[Title]
> > ,[BirthDate]
> > ,[MaritalStatus]
> > ,[Gender]
> > ,[HireDate]
> > ,[SalariedFlag]
> > ,[VacationHours]
> > ,[SickLeaveHours]
> > ,[CurrentFlag]
> > ,[rowguid]
> > ,[ModifiedDate]
> > FROM [AdventureWorks].[HumanResources].[Employee]
> > WHERE [EmployeeID] = @empid OR @empid IS NULL
> >
> >
> > EXEC usp_GetEmployee 1
> >
> > EXEC usp_GetEmployee NULL
> >
> > EXEC usp_GetEmployee
> >
> > John
> >

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15068 seconds with 12 queries