Home All Groups Group Topic Archive Search About

IF Statement - Whats Next

Author
1 Sep 2006 7:06 PM
Stephen Lynch
Can I use an If statement on a StProc?  I am erroring around the IF.

USE DataInput

GO

CREATE PROC a_spAppendtoManualContribution

@CompanyId int

IF

(SELECT COUNT(*) AS 'Number of Rows'

FROM ManualContribution

WHERE

CompanyId = @Company)>0

/* Insert Records into the Manual Contribution Table */

INSERT INTO ManualContribution

(CompanyId,EmployeeUID,FullName,Post)

SELECT Employees.CompanyId,Employees.EmployeeUID,Employees.Last +', '+
Employees.First +' '+ ISNULL( Employees.Middle,'' ) AS 'FullName','0'

FROM Contributions, Employees

WHERE CompanyId = @CompanyId AND

Employees.AllowContributions = 1

ORDER BY FullName



Thanks



Steve

Author
1 Sep 2006 7:11 PM
Arnie Rowland
You are missing the keyword [AS]

CREATE PROCEDURE dbo.MyProcedure
   ( @Parameter  datatype )
AS
   IF ...

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


Show quote
"Stephen Lynch" <raider1rai***@yahoo.com> wrote in message
news:44f884cb$0$3571$815e3792@news.qwest.net...
> Can I use an If statement on a StProc?  I am erroring around the IF.
>
> USE DataInput
>
> GO
>
> CREATE PROC a_spAppendtoManualContribution
>
> @CompanyId int
>
> IF
>
> (SELECT COUNT(*) AS 'Number of Rows'
>
> FROM ManualContribution
>
> WHERE
>
> CompanyId = @Company)>0
>
> /* Insert Records into the Manual Contribution Table */
>
> INSERT INTO ManualContribution
>
> (CompanyId,EmployeeUID,FullName,Post)
>
> SELECT Employees.CompanyId,Employees.EmployeeUID,Employees.Last +', '+
> Employees.First +' '+ ISNULL( Employees.Middle,'' ) AS 'FullName','0'
>
> FROM Contributions, Employees
>
> WHERE CompanyId = @CompanyId AND
>
> Employees.AllowContributions = 1
>
> ORDER BY FullName
>
>
>
> Thanks
>
>
>
> Steve
>
>
Author
1 Sep 2006 7:12 PM
Tibor Karaszi
You are missing AS after the parameter list. Also the ORDER BY in the SELECT statement for the
INSERT is meaningless.

Show quote
"Stephen Lynch" <raider1rai***@yahoo.com> wrote in message
news:44f884cb$0$3571$815e3792@news.qwest.net...
> Can I use an If statement on a StProc?  I am erroring around the IF.
>
> USE DataInput
>
> GO
>
> CREATE PROC a_spAppendtoManualContribution
>
> @CompanyId int
>
> IF
>
> (SELECT COUNT(*) AS 'Number of Rows'
>
> FROM ManualContribution
>
> WHERE
>
> CompanyId = @Company)>0
>
> /* Insert Records into the Manual Contribution Table */
>
> INSERT INTO ManualContribution
>
> (CompanyId,EmployeeUID,FullName,Post)
>
> SELECT Employees.CompanyId,Employees.EmployeeUID,Employees.Last +', '+ Employees.First +' '+
> ISNULL( Employees.Middle,'' ) AS 'FullName','0'
>
> FROM Contributions, Employees
>
> WHERE CompanyId = @CompanyId AND
>
> Employees.AllowContributions = 1
>
> ORDER BY FullName
>
>
>
> Thanks
>
>
>
> Steve
>
>
Author
1 Sep 2006 7:18 PM
Arnie Rowland
Steven,

One other comment about your [IF] statement. Using the various forms of count() requires the Query Processor to scan all of the data (that meets the filter criteria). If you were to use [IF EXISTS] instead, the QP will stop looking as soon as it determines that there is just one row that meet the filter criteria.

So,

IF EXISTS ( SELECT ... )
   {do something}

may be more efficient than,

IF (SELECT count(*)...) > 0
   {do something}

Especially when handling large tables, or large numbers or rows meeting the filter criteria.

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


Show quote
"Stephen Lynch" <raider1rai***@yahoo.com> wrote in message news:44f884cb$0$3571$815e3792@news.qwest.net...
> Can I use an If statement on a StProc?  I am erroring around the IF.
>
> USE DataInput
>
> GO
>
> CREATE PROC a_spAppendtoManualContribution
>
> @CompanyId int
>
> IF
>
> (SELECT COUNT(*) AS 'Number of Rows'
>
> FROM ManualContribution
>
> WHERE
>
> CompanyId = @Company)>0
>
> /* Insert Records into the Manual Contribution Table */
>
> INSERT INTO ManualContribution
>
> (CompanyId,EmployeeUID,FullName,Post)
>
> SELECT Employees.CompanyId,Employees.EmployeeUID,Employees.Last +', '+
> Employees.First +' '+ ISNULL( Employees.Middle,'' ) AS 'FullName','0'
>
> FROM Contributions, Employees
>
> WHERE CompanyId = @CompanyId AND
>
> Employees.AllowContributions = 1
>
> ORDER BY FullName
>
>
>
> Thanks
>
>
>
> Steve
>
>
Author
1 Sep 2006 7:24 PM
Stephen Lynch
Thanks,
I'll try it
  "Arnie Rowland" <ar***@1568.com> wrote in message news:uQIEktfzGHA.3512@TK2MSFTNGP04.phx.gbl...
  Steven,

  One other comment about your [IF] statement. Using the various forms of count() requires the Query Processor to scan all of the data (that meets the filter criteria). If you were to use [IF EXISTS] instead, the QP will stop looking as soon as it determines that there is just one row that meet the filter criteria.

  So,

  IF EXISTS ( SELECT ... )
     {do something}

  may be more efficient than,

  IF (SELECT count(*)...) > 0
     {do something}

  Especially when handling large tables, or large numbers or rows meeting the filter criteria.

  --
  Arnie Rowland, Ph.D.
  Westwood Consulting, Inc

  Most good judgment comes from experience.
  Most experience comes from bad judgment.
  - Anonymous


Show quote
  "Stephen Lynch" <raider1rai***@yahoo.com> wrote in message news:44f884cb$0$3571$815e3792@news.qwest.net...
  > Can I use an If statement on a StProc?  I am erroring around the IF.
  >
  > USE DataInput
  >
  > GO
  >
  > CREATE PROC a_spAppendtoManualContribution
  >
  > @CompanyId int
  >
  > IF
  >
  > (SELECT COUNT(*) AS 'Number of Rows'
  >
  > FROM ManualContribution
  >
  > WHERE
  >
  > CompanyId = @Company)>0
  >
  > /* Insert Records into the Manual Contribution Table */
  >
  > INSERT INTO ManualContribution
  >
  > (CompanyId,EmployeeUID,FullName,Post)
  >
  > SELECT Employees.CompanyId,Employees.EmployeeUID,Employees.Last +', '+
  > Employees.First +' '+ ISNULL( Employees.Middle,'' ) AS 'FullName','0'
  >
  > FROM Contributions, Employees
  >
  > WHERE CompanyId = @CompanyId AND
  >
  > Employees.AllowContributions = 1
  >
  > ORDER BY FullName
  >
  >
  >
  > Thanks
  >
  >
  >
  > Steve
  >
  >

AddThis Social Bookmark Button