How to select the entire row in sql using query - sql

I have a table name FooTable in which I have n columns, but I want to search a particular entire column using query. How can I do that?
For ex:
field1 field2 field3 .................... fieldn
hey hi whats
hello bye go
.. .. ..
.. .. ..
n n n
Select *from FooTable
I want to search the entire column 3 items, so how should I make the query so that all the columns 3 items are shown?

Question isn't very clear. You might mean this
SELECT * FROM FooTable
gets all
SELECT field1, field2, field3 FROM FooTable
gets those columns

You have to provide additional WHERE clause with specific value in that row
example SELECT * FROM table WHERE field1 = "Hello";

you have to define a variable
select #var += field3 from footable

Related

SQL select into syntax

I'm trying to create a temporary table with a select into statement in a stored procedure as follows:
SELECT *
INTO #GENEALOGY
FROM
(
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
FROM table
WHERE condition
)
Any command I type after this closing bracket fails the syntax check (such as another SELECT statement).
I've tried putting BEGIN and END before and after the whole statement and then starting my next command.
I've tried adding
AS tablename
after the closing bracket and then the next statement but it doesn't like that either
I've tried removing the # but same problem.
I actually need to run a WHILE loop after this and INSERT more records into the same table.
What am i doing wrong?
thanks
Out of curiosity, why are you using a subquery?
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
INTO #GENEAOLOGY
FROM table
WHERE condition;
The structure of your query is ok. You should include a semi-colon at the end, if this is in a programming block. I assume table is a real name and that the other parts of the query are syntactically correct.
What does the error mention? If you run this in SSMS more than once, the second time the temp table already exists and could be an error.
You said an alias was not enough, but this is a right sentence:
SELECT *
INTO #GENEALOGY
FROM
(
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
FROM table
WHERE condition
) as TableNameAlias
thanks guys for the help
in the end I changed to an INSERT INTO rather than a SELECT INTO because
a) i needed to do 2 different select queries to get the data i need (1 for the first record and a 2nd for all subsequent records) and the second query would have had to be an INSERT anyway
b) and i wasn't sure how long sql server would keep the temp table open if i was calling the procedure from a crystal report
so i created a fixed table, use SELECT INTO and just delete all records as first line of my sp to clear out unwanted data
ta!

SQL Select Query With FIND_IN_SET Or Any things else to find comma separated values in a column with comma separeted values

I have one column having comma separated values:
id Column_name
== ==========
1 value1,value2,value3,value5
2 value2,value3,value4
3 value1,value3,value5
4 value1,value2,value4,value5
Now, I want a query that get all records having value2 OR value4
I need query like below if possible:
SELECT * FROM table_name WHERE FIND_IN_SET('value2,value4',`column_name`)
So, it will return recode with id 1,2,4 (having value2 or value4)
I have form for user that send value1,value2 ..etc from check_box
How can I optimize my select Query?
Use it like this
SELECT * FROM table_name
WHERE FIND_IN_SET('value2',column_name) > 0
or FIND_IN_SET('value4',column_name) > 0
But never, never, never store multiple values in one column!
You should actually rather change your DB structure to something like this
your_table
----------
id
name
other_columns
values table
------------
your_table_id
value

Change only single column values when we use * in sql

Is there a way to change only single column values as updated in selection of oracle sql.
SELECT ORDD.id||'sdaf' as id,ORDD.*
FROM RDT_ORDER ORDM,RDT_ORDERDETAIL ORDD;
Here in RDT_ORDERDETAIL am having around Hundered columns but only single coulmn I need as updated.
I don't want to select all Hundred columns by writing all columns in select query as below
SELECT ORDD.id||'sdaf' as id,ORDD.col1,ORDD.col2,ORDD.col3,ORDD.col4
FROM RDT_ORDER ORDM,RDT_ORDERDETAIL ORDD;
Please note I have removed where clause,I need to get single updated column values other columns as it is.But I shouldn't write all coulmns in Select.
Existing DB Records :
ID NAME COL3 COL4
1 name1 .. ..
2 name2 .. ..
3 name3 .. ..
Now Result :
ID ID NAME COL3 COL4
1_'sdaf' 1 name1 .. ..
2_'sdaf' 2 name2 .. ..
3_'sdaf' 3 name3 .. ..
Expected Result :
ID NAME COL3 COL4
1_'sdaf' name1 .. ..
2_'sdaf' name2 .. ..
3_'sdaf' name3 .. ..
If you want to select all but one rows then the answer is No - you either get all fields (*) OR specify the fields you want. This is how Oracle works.
If you still persists, then there might be some work around like Dynamic SQL using query generator or data cartridge
You can refer here for similar question.
If you want to have a string manipulation and all columns you can do using alias.
SELECT COL1||'_asd_', T.*
FROM MYTABLE T;
Your idea of getting all columns from a table with a manipulation on one or more columns without writers cramp is not possible directly. You may try dynamic SQL but I will not suggest that for anything because of low maintainability and its a head ache on a long run.
I don't think you can exclude a single column by quering with select * from.... May be a VIEW can help you on this.
CREATE OR REPLACE VIEW <view_name> AS
SELECT ordd.id||'sdaf' AS id, ordd.col1, ordd.col2, ordd.col3, ordd.col4
FROM rdt_order ordm,
rdt_orderdetail ordd
WHERE .....;

Attach query string to results

When doing a sql query with something like
SELECT * FROM table1 WHERE column1 LIKE 'searchstring';
Is there a way to attach the query string, here 'searchstring' to the results? I ask because in this case
SELECT * FROM table1 WHERE column1 LIKE '%searchstring1%' OR column1 LIKE '%searchstring2%';
you can get multiple rows back in the result, and you may not know which rows go with which query string.
Is there a way to attach the query strings to their associated rows in another column? For example,
result_col1 result_col2 searched
a b searchstring1
c d searchstring1
f g searchstring2
You can include the searchstring as a column in your query and obtain them in your results:
SELECT 'searchstring' AS querystring, * FROM table1 WHERE column1 LIKE '%searchstring%';

Minus operator in sql

I am trying to create a sql query with minus.
I have query1 which returns 28 rows with 2 columns
I have query2 which returns 22 row2 with same 2 columns in query 2.
when I create a query query1 minus query 2 it should have only show the 28-22=6 rows.
But it showing up all the 28 rows returned by query1.
Please advise.
Try using EXCEPT instead of MINUS. For Example:
Lets consider a case where you want to find out what tasks are in a table that haven't been assigned to you(So basically you are trying to find what tasks could be available to do).
SELECT TaskID, TaskType
FROM Tasks
EXCEPT
SELECT TaskID, TaskType
FROM Tasks
WHERE Username = 'Vidya'
That would return all the tasks that haven't been assigned to you. Hope that helps.
If MINUS won't work for you, the general form you want is the main query in the outer select and a variation of the other query in a not exists clause.
select <insert list of fields here>
from mytable a
join myothertable b
on b.aId = a.aid
where not exists (select * from tablec c where a.aid = c.aid)
The fields might not be exactly alike. may be one of the fields is char(10) and the other is char(20) and they both have the string "TEST" in them. They might "look" the same.
If the database you are working on supports "INTERSECT", try this query and see how many are perfectly matching results.
select field1, field2 from table1
intersect
select field1, field2 from table2
To get the results you are expecting, this query should give you 22 rows.
something like this:
select field1, field2, . field_n
from tables
MINUS
select field1, field2, . field_n
from tables;
MINUS works on the same principle as it does in the set operations. Suppose if you have set A and B,
A = {1,2,3,4}; B = {3,5,6}
then, A-B = {1,2,4}
If A = {1,3,5} and B = {2,4,6}
then, A-B = {1,3,5}. Here the count(A) before and after the MINUS operation will be the same, as it does not contain any overlapping terms with set B.
On similar lines, may be the result set obtained in query 2 may not have matching terms with the result of query1. Hence you are still getting 28 instead of 6 rows.
Hope this helps.
It returns the difference records in the upper query which are not contained by the second query.
In your case for example
A={1,2,3,4,5...28} AND B={29,30} then A-B={1,2,3....28}