Solved

t-sql query

Posted on 2014-01-09
1
287 Views
Last Modified: 2014-01-09
I'm using sql server 2008 R2

I have a test table called TestEmployeeHistory

The table looks like this:

my table
This is the script to create the table.

CREATE TABLE [dbo].[TestEmployeeHistory](
	[Region] [smallint] NULL,
	[SocialNo] [char](9) NULL,
	[EmployeePaidDate] [smalldatetime] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[TestEmployeeHistory] ([Region], [SocialNo], [EmployeePaidDate]) VALUES (1, N'12345678 ', CAST(0x8E1302D0 AS SmallDateTime))
INSERT [dbo].[TestEmployeeHistory] ([Region], [SocialNo], [EmployeePaidDate]) VALUES (1, N'12345679 ', CAST(0x8E1302D0 AS SmallDateTime))
INSERT [dbo].[TestEmployeeHistory] ([Region], [SocialNo], [EmployeePaidDate]) VALUES (1, N'12345680 ', CAST(0x8E1302D0 AS SmallDateTime))
INSERT [dbo].[TestEmployeeHistory] ([Region], [SocialNo], [EmployeePaidDate]) VALUES (1, N'12345681 ', CAST(0x8E1302D0 AS SmallDateTime))
INSERT [dbo].[TestEmployeeHistory] ([Region], [SocialNo], [EmployeePaidDate]) VALUES (1, N'12345682 ', CAST(0x912B02D0 AS SmallDateTime))

Open in new window


I have a query that looks like this:

DECLARE @Region varchar(800), @FromDate char(30), @ToDate char(30), @EmployeeSocial char(9)
SET @Region = '1'
SET @FromDate = '07/01/1999'
SET @ToDate = '06/30/2000'
SET @EmployeeSocial = NULL

SELECT h.Region
      ,h.SocialNo
      ,h.EmployeePaidDate
FROM  [TestDatabase].[dbo].[TestEmployeeHistory] h
WHERE
           IF (@Region <> '0')
             BEGIN
	             h.Region IN (RTrim(@Region)) AND 
             END
           If (@EmployeeSocial <> '')
             BEGIN
	             h.SocialNo = @EmployeeSocial AND
             END

GROUP BY h.Region
        ,h.SocialNo
        ,h.EmployeePaidDate

Open in new window


When I run this query I get this error message.

my error message
It seems I am making a mistake in the syntax of my if statements

Anyone where I'm making a mistake in my if statements?
0
Comment
Question by:maqskywalker
1 Comment
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 39769605
You can't use "IF" within a query.  You need to use "CASE" instead, or just write the WHERE conditions to do what you need.


WHERE
    ( @Region = 0 OR h.Region IN (RTrim(@Region)) ) AND
    ( @EmployeeSocial = '' OR h.SocialNo = @EmployeeSocial )
GROUP BY h.Region
        ,h.SocialNo
        ,h.EmployeePaidDate
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now