views:

61

answers:

2

I was using MySQL's concat_ws function to get data from MySQL database and it was working perfectly. I discover this query through this SO Question. Now I have same case but this time database is SQL Server.

Asking again for SQL Server:

I have a table like this:

id | roll_no | name
---------------------
 1 |   111   | Naveed
 2 |   222   | Adil
 3 |   333   | Ali 

If I have data like this:

$fields = array( "id" , "roll_no" ) and $values = array( "1,111", "2,222" );

It means I have to write a sql query to get records from table where (id != 1 and roll_no != 111) and (id != 2 and roll_no != 222). It means 3rd record will be fetched.

I have data like this:

$fields = array( "id" ) and $values = array( "2", "3" );

It means I have to write a sql query to get records from table where (id != 2) and (id != 3). It means 1st record will be fetched.

How to write a general single query to get data from table using above two data arrays?

Thanks

+1  A: 

Your question is unclear for me, but from the link you provided to the other SO question, and assuming you just want to adapt to SQL Server, I guess you could just replace

concat_ws (',', id, roll_no)

by

id + ',' + roll_no
iDevlop
Actually I am using this query in my PHP code in which **fields** and **values** are passed by variables. And variables are created by using data arrays provided in my question. I want to know a best method that will create the fields and values string such that it fit in query perfectly for any amount of fields and values. Same issues was solved in my linked mysql question by using concat_ws function but concat_ws function is not available in SQL Server. Any way +1 for your interest.
NAVEED
+1  A: 

It's a simple sql. Do you know 'where (not) in' clause?

Like for the first one if you have 1-to-1 relation between id and roll no, you can simple write

select * 
from table 
where id not in ('1','2') and roll_no not in ('111','222')

For the second it's easier.

Muktadir
If you post code or XML, **please** highlight those lines in the text editor and click on the "code" button (101 010) on the editor toolbar to nicely format and syntax highlight it!
marc_s
Yes it is simple when you are hard coding it in query. But I have to use this query in my PHP code for different amount of fields and values. So I want to make this query general in my code for any amount of fields/values combination. Same issue was solved in my provided linked question with mysql concat_ws function.
NAVEED
you can write strings in the query with php, can't you? And this is a SQL which he needed. concat_ws is not SQL, dude!
Muktadir