SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Usingh LIMIT and UNION together
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ajaypal
Starting Member

Canada
6 Posts

Posted - 07/22/2013 :  15:49:19  Show Profile  Reply with Quote
Hi ,

I have a extremely large table and out of that I just need 2 records but based on some logic . For example
IF EMPLOYEE_ADDRESS <> NULL
set Employee_HAS_HOME to 1
ELSE
set Employee_HAS_HOME to 0

here I can not use just first 1000 records as I am not sure If I will cover the second condition in first 1000 rows . Then I came up with solution like : -
select * from employee_table where employee_address <> NULL LIMIT 1
UNION
select * from employee_table where employee_address = NULL LIMIT 1

And this will give me 2 records which I can use for my testing .But unfortunately this is not working. Could you please suggest me some other efficient way or tell me what is wrong in the query

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 07/22/2013 :  15:55:14  Show Profile  Visit SwePeso's Homepage  Reply with Quote
UPDATE dbo.Table1 SET Employee_Has_Home = CASE WHEN Employee_Address IS NULL THEN 0 ELSE 1 END



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

ajaypal
Starting Member

Canada
6 Posts

Posted - 07/22/2013 :  16:02:31  Show Profile  Reply with Quote
Slight Change , there is a existing table and the condition is on this existing table. Based on the result I have to check the data in the new table (data already exists but for me to test if data has been migrated successfully , I have to check both the cases) : -

IF existing_Table.EMPLOYEE_ADDRESS <> NULL
set NEW_Table.Employee_HAS_HOME to 1
ELSE
set NEW_Table.Employee_HAS_HOME to 0
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 07/22/2013 :  16:06:29  Show Profile  Visit SwePeso's Homepage  Reply with Quote
UPDATE		newtable
SET		newtable.Employee_Has_Home =	CASE
							WHEN oldtable.Employee_Address IS NULL THEN 0
							ELSE 1
						END
FROM		dbo.Table1 AS newtable
INNER JOIN	dbo.Table2 AS oldtable ON oldtable.EmployeeID = newtable.EmployeeID;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 07/23/2013 :  01:59:10  Show Profile  Reply with Quote
quote:
Originally posted by ajaypal

Slight Change , there is a existing table and the condition is on this existing table. Based on the result I have to check the data in the new table (data already exists but for me to test if data has been migrated successfully , I have to check both the cases) : -

IF existing_Table.EMPLOYEE_ADDRESS <> NULL
set NEW_Table.Employee_HAS_HOME to 1
ELSE
set NEW_Table.Employee_HAS_HOME to 0


Use earlier provided suggestion
On a sidenote, please be aware that you cant use =,<> etc opertors with NULL values. to check for NULLs use IS NULL and IS NOT NULL conditions as in previous suggestion

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 07/23/2013 :  03:13:49  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Visakh, please be restrained with that advice.
We don't know what ANSI_NULL setting the person has.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 07/23/2013 :  03:55:51  Show Profile  Reply with Quote
quote:
Originally posted by SwePeso

Visakh, please be restrained with that advice.
We don't know what ANSI_NULL setting the person has.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA


I think OP's on MySQL (see usage of LIMIT)
In MySQL, you cannot turn off ANSI_NULLS settings
So OP has to use IS NULL and IS NOT NULL always





------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000