Solved

t-sql query

Posted on 2014-01-09
1
296 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:
Scott Pletcher 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

772 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