Do I need a stored procedure - sql

I have three tables - T1,T2 & T3.
For each row in T1 I need to fetch the all the data from that table and a few other columns from a third table T3 and insert into T2 //Which is partitioned version of T1
Do I need stored procedure for this?

No you don't , it can be done with a simple insert as select:
INSERT INTO T2
SELECT t1.*,t3.col1,t3.col2...
FROM T1
LEFT OUTER JOIN t3
ON(t1.ID? = t3.ID?)
Of course you have to change this query to whatever columns you want, and the join condition to the relations between the tables.

Related

Is there any alternative way of achieving below logic in sql?

I have two tables -> tb1 and tb2.
I am performing left join operation on these tables using ID column and also i have one more condition such as one column is not equal to other column .
Below is sample code
select * from tb1 LEFT JOIN tb2 ON tb1.id=tb2.id AND tb1.pid!=tb2.pid;
I am able to get results from above query.
But i need to know is there any alternate ways to get same result using sql.?
The actually SQL standard uses <> instead of !=.
select * from tb1 LEFT JOIN tb2 ON tb1.id=tb2.id AND tb1.pid<>tb2.pid;
It seems to you not equal not working because of your join and join condition.
if we create two tables and create like your query
create table t1(id int,pid int);
create table t2 (id int,pid int );
insert into t1 values(1,2),(2,3),(3,4);
insert into t2 values(1,2),(2,3),(3,4);
select t1.* from t1 left join
t2 on t1.id=t2.id and
t1.pid!=t2.pid
order by t1.id
id pid
1 2
2 3
3 4
It returns all the values of 1st table, because LEFT JOIN returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.
But if you put inner join in the same it will not return any row. so i think problem is not in the "not equal operator"

Selecting rowset when value exists in one of 5 tables with different amounts of columns

Using SQL Server, I Need to return the entire row from whatever table contains 'value' in the Filename column (A column each of the tables contain), but the tables do not have the same number of columns, and each table has unique columns with their own specific data types (The only column Name/Type they have in common is the Filename column that I need to check for 'value').
Ideally, I would be able to do something along the lines of:
SELECT * FROM Table1, Table2, Table3, Table4, Table5
WHERE Filename = 'someValue'
Since all tables share the same column name for the Filename.
I have tried using Union but have issues since the number of columns and datatypes of the tables do not align.
I have also tried every combination of JOIN I could find.
I'm sure this could be accomplished with IF EXISTS, but that would be many, many lines of what seems like unnecessary code. Hoping there is a more elegant solution.
Thanks in advance!
You can try to join the tables together. First create temporary table where you store the input. And then join the tables with this temporary to get all records you want. When there is no record for that filename in the table, then you will get NULL values.
create table Table1 (id int,value int);
insert into Table1 values (1,10)
create table Table2 (id int,value int);
insert into Table2 values (1,20)
create table Table3 (id int,value int);
insert into Table3 values (2,30)
Here is the query itself
create table #tmp (id int)
insert into #tmp
values (1)
select t.id, t1.value, t2.value, t3.value from #tmp as t
left join Table1 as t1
on t.id = t1.id
left join Table2 as t2
on t.id = t2.id
left join Table3 as t3
on t.id = t3.id
And this is what you get
id value value value
1 10 20 NULL
this should work too:
EXEC sp_MSforeachtable
#command1='SELECT * FROM ? where filename = ''someValue''',
#whereand='AND o.id in (select object_id from sys.tables where name in (''Table1'',''Table2'',''Table3''))'

Compare two tables and insert the matching content to another table

I have been working on this problem for 3 months now and gave up once or twice. Yes, I am a novice. I created 3 tables with data. Table 1 has a letter and number. Table 2 has a name, letter and number. Table 3 has the end result. I want to compare the T1 and T2. If the name and number in T1 matches a name and number in table 2. I want the result to in T3 to include name, letter and number. This is what I have so far but it is not working.
SELECT * FROM T1 and SELECT * FROM T2
WHEN
TABLE T1(letter) && TABLE T2(letter)
AND
TABLE T1(number) && TABLE T2(letter)
INSERT INTO TABLE T3 (name,letter,number)
What you need is to do an inner join of the first and second tables based on the attributes name and letter.
SELECT T2.name, T2.letter, T2.number
FROM T1
INNER JOIN T2
ON T1.letter=T2.letter AND T1.number=T2.number;
For more details, you can refer http://www.w3schools.com/sql/sql_join.asp
To expand upon Dinesh's answer,
You'll need an inner join for this. Inner joins gives you rows that match the columns in both tables you've specified.
You can then combine it into an insert statement to put it into your T3 table. So this is one complete SQL statement:
INSERT INTO T3
SELECT T2.name, T2.letter, T2.number
FROM T2
INNER JOIN T1
ON T2.letter = T1.letter
AND T2.number = T1.number;
As a side note, there's also left joins and right joins (and heaps more). Think of Left, Inner, Right joins as two circles in a venn diagram.

How to update Two Columns at the same time with different queries

I have this after insert trigger which updates two different columns based on a join. Basically it turns an Id into a value. This works fine except when one of the Ids does not match (ie, it's zero for the default) Then neither is updated. If the join fails, it should just insert null.
CREATE TRIGGER [AfterHistoryInsert]
ON [Jet].[HistoryEntity]
FOR INSERT
AS
BEGIN
Update t1 Set t1.OldValue = t2.Value, t1.NewValue = t3.Value
From Jet.HistoryEntity t1
join Jet.LookupListItemEntity t2 on Cast(t1.OldValue as int) = t2.Id
join Jet.LookupListItemEntity t3 on Cast(t1.NewValue as int) = t3.Id
inner join inserted i on i.Id = t1.Id
where t1.FieldName like '%Id'
END
Greg
Try left outer join instead join
If you are updating into a bigger table, one of the suggestion will be:
- Selecting all the columns that you want to update(In your case Value) into temp table
- Update Statement by using left outer join to temp table.

In SQL Server, How to join two table having different column name and different data row?

Suppose I have two tables T1 & T2. I want resulting output table as O1 with help of a SQL query
T1 {SName}
T2 {VName}
O1 {SName, VName}
It seems like you want a cross join of the two tables to include all combinations of rows in T1 and T2. Try this:
Select SName, VName From T1 Inner Join T2 On 1=1
The number of rows you will get is the product of the number of rows in T1 and T2 each.
if you're not joining on anything and want a table of all possible combinations:
select SName, VName
into O1
from T1 cross join T2
if you have a column to join on:
select SName, VName
into O1
from T1 inner join T2 on T1.col = T2.col
Select record from T1 and T2 based on filtering criteria and then insert record in table O1 , use below query to create table O1 and inserting those records.
INSERT INTO O1(SNAME, VNAME)
SELECT(T1.SNAME, T2.VNAME)
From T1 Inner Join T2 On 1=1 //i.e WHERE T1.id=T2.T1_id
use WHERE to filter records
there are several ways of doing it.one easy way is:
select T1.SName,T2.VName from T1,T2;
i don't know if i am right, you can use cross join.
if you have two tables Players and GameScores then
SELECT* INTO O1 FROM GameScores CROSS JOIN Players will return all records where each row from the first table is combined with each row from the second table. Which also mean CROSS JOIN returns the Cartesian product of the sets of rows from the joined tables.
to get the above result as
T1 {SName}
T2 {VName}
O1 {SName, VName}
(SELECT * FROM T1,T2) as O1;
will definitely work if both the table have single value if not then you can select the
Particular column like T1.SName,T2.VName