Selecting Primary Key Value Into Another Table

Apr 11, 2008

I have table A with Primarykey column, AId, Identity field.

I have table B with foriegn key column , AId,

I have same number of rows in both tables (over million), but in Table B, column AId is null at present, as it was added later.

Now I need to select all AId values and update them in existing rows.

Any idea, how would my T-SQL look like???

Many Thanks,

View 9 Replies


ADVERTISEMENT

Need Help Selecting Primary Key!

Mar 10, 2008



Hello!

I´m having some trouble choosing my primary key for a database I´m currently developing. I´m not that experienced with developing databases, although this is not my first.


I have an OrderLookup table, where the PK is the PurchaseOrderID.
I also have an OrderInput table, where the user input will be stored.
The PurchaseOrderID will exist in this table also, and all records
must have a corresponding PurchaseOrderID in the OrderLookup table.


My issue here is that in the OrderInput table, one PurchaseOrderID
can exist many times, so I´m not really sure how to set up my PK for this table.

I´m thinking maybe I should just create an identity column and use as PK,
and have PurchaseOrderID be a FK referencing OrderLookup.
Would that be a good idea? Since like 98% of all queries will have a "where PurchaseOrderID = xxxx", would it be a good idea to move the clustered index from the PK to the FK?


Other thoughts?

Thanks in advance!

View 4 Replies View Related

Adding Primary Key To A Table Which Has Already A Primary Key

Aug 28, 2002

Hi all,
Can anyone suggest me on Adding primary key to a table which has already a primary key.

Thanks,
Jeyam

View 9 Replies View Related

Selecting ONLY Records From One Table Having ALL Data Contained In Other Table (GROUP BY?)

Jul 20, 2005

Hello everyone,Small and (I think) very simple quesiton;-) which makes me creazy.Let's say I have two tables listed below:T1====IDX====134T2===============IDD fk_IDX===============A1A2A4B1B3B4C4D1D2D3D4I would like to select from table T2 all distinct records IDD whichhave all of fk_IDX containded in T1.The select statement should return in this case ONLY:B and Dbecasue:B has 1,3,4andD has 1,2,3,4 so it has this combination 1,3,4 contained in the T1also.I've tried to do that with group by, with having, in and it neverworks (I always became all records which one of them is in this T1table).Maybe some one from you did try something like that, and can give afast answer.I will be very greatfullGreatingsMateusz

View 2 Replies View Related

Selecting SUM From One Table As Field In Results Of Rows From Another Table.

Nov 20, 2007

Hi,
These aren't my tables, but lets dumb it down for my sake!

I have 2 tables:

CompanyInfo: IDNumber, EffectiveDate, CompanyName
TransacationInfo: TransactID, CompanyID, TransDate, Amount

There are several records in TransactionInfo for each record in CompanyInfo.

I want to:

SELECT CompanyInfo.IDNumber, CompanyInfo.EffectiveDate, CompanyInfo.CompanyName,
SUM(TransactionInfo.Amount)

How can I do this? Can I?

View 4 Replies View Related

Selecting From A Table, When I Only Have The Table Name As A String Format

Apr 7, 2006

is there some way to allow this to execute, using a function or something?:



select * from 'cusomer'



you see i have a list of about 300 table name in string format, and i want to use a cursor to itterate through each table, but as all the table names are in string format i cant get it done. any ideas??

View 1 Replies View Related

Selecting 2 F/k's From Same Table?

Sep 13, 2005

This is, I'm hoping, a simple thing that I just don't know how to do. But I've searched my books and read a lot of threads but so far, no luck in what I'm trying to do. Here's the basic layout of 2 tables from my d/b (MSDE):

tblCity
cityID (p/k)
stateID (f/k to tblState.stateID)
cityName (varchar 35)

tblStudent
studID (p/k)
cityID (f/k to tblCity.cityID)
hometownID (f/k to tblCity.cityID)
studFName (varchar 25)
studLName (varchar 35)
...

I *hope* that what I tried to do there is allowed. Basically what I'm trying to do is to tie the student to both his current City (by foreign key cityID) and his hometown city (also by foreign key cityID).

So, if New York is (127, 33, 'New York') and Birmingham is (1050, 1, 'Birmingham') (in tblCity), then in the student table (tblStudent), John Smith would be
Quote: 12150, 127, 1050, 'John', 'Smith', ...
How would I format my SQL statement to get both cityNames? I thought this would work


Code:

SELECT s.studID, c.cityID, c.hometownID,
s.studLName + ', ' + s.studFName AS studNameName,
c.cityName, c.cityName AS swapconNum
FROM tblStudent AS s INNER JOIN tblCity AS c
ON (s.cityID = c.cityID)
WHERE s.studID = 12150


Can I do this? If so, what am I doing wrong? Sorry, I'm kinda sorta new to SQL.

RudeDog

View 2 Replies View Related

Query Based Off Primary Key Of Parent Table - Adding Child Table

Jan 28, 2012

I need to add a child table that will tell us who the participants counselor is, what I did was I did a Make Table query based off the primary key of the Parent table and made that the link (foreign key) for the People_tbl and the Counselor_tbl, so if the counselor changes then the user adds the record to the counselor tbl and then puts in the Effective date. The problem is that when I run a report it doesn't show the present counselor always shows the old counselor?

Code:
SELECT Student_ind.StudentFirstName, Student_ind.StudentLastName, Student_ind.[Student ID], People_tbl.[Family ID], People_tbl.FirstName,
People_tbl.LastName, People_tbl.[Parent ID]
FROM People_tbl RIGHT OUTER JOIN
Student_ind ON People_tbl.[Family ID] = Student_ind.[Family ID]
WHERE (People_tbl.LastName = @Enter_LastName) AND (People_tbl.FirstName = @Enter_FirstName)

View 5 Replies View Related

SQL Server Admin 2014 :: Few Record Loss In Table Primary Key Where Same Records Exists In Foreign Key Table?

Jun 21, 2015

Previously same records exists in table having primary key and table having foreign key . we have faced 7 records were lost from primary key table but same record exists in foreign key table.

View 3 Replies View Related

Selecting From One Table Inserting Into Another

Feb 18, 2007

hi, i am sure this is very simple, but its just got me stuck! i have a table which i use for inserting products, now its not very exciting to see the cat_id so i wanted to put a dropdown box on there which would show the cat_description from tbl_cat and then put the tbl_cat.cat_id into tbl_products.cat_id
any help would be greatful
 Jez

View 1 Replies View Related

Selecting From One Table, Inserting Into Another

Jul 3, 2007

hi there, i have a page where on my button click i need to select address, pcode from tbl_person where person_id = querystring and update or insert those values into tbl_del where order_ID = querystring 2 below is the code i am using, although im stuck on the sql stringSqlConnection objConnAddy = new SqlConnection(sConnectionStringCustInfo);using (objConnAddy)
{
objConnOID.Open();
// insert values into delivery table, second line updates price + VAT
string sqlAddy = "?????";
SqlCommand objCmdAddy = new SqlCommand(sqlAddy, objConnAddy);
objCmd1.ExecuteNonQuery();
i had a look on the net and here but i cant seem to find exactly what i want, any help would be great!
Cheers Jez

View 2 Replies View Related

Selecting FieldName From The Table

Sep 23, 2007

How can i just select filed name from the name without data. What query should we write foe that.

View 2 Replies View Related

Selecting One Row And Inserting It Into Another Table One By One

Feb 14, 2008

i ve a table named "Employee" in wh i ve fields "Emp_Id","Name" and "Email". i want to select each employee "Emp_Id" one by one from "Employee" table and insert it into another table named "Concerned_Department". like if i ve three rows in "Employee" table with "Emp_Id" 1,2 and 3 respectively,in this case i want to first select "Emp_Id"=1 then "Emp_id"=2 and lastly "Emp_id"=3 and insert them into "Concerned_Department"table.
 after that the process starts again like inserting "Emp_Id"=1 in "Concerned_Department" and so on.
plz note that i want to insert "Emp_Id" one by one not all the fields together.
 can u plz tell me the query for that wh i ve to write in SQL
Thanks and happy valentine day to all of u
 regards
Ahmed Bilal Jan

View 3 Replies View Related

Randomly Selecting From A Table.

May 3, 2004

lets say that this select statement will give me 100 records..

"Select UserID from Users where location = 'FL'"

from these 100 records, I want to randomly get 5 records. whats the best way to go about doing this

View 1 Replies View Related

Selecting Records In One Table But Not In Another 2

Dec 13, 2004

Hi, my sql is not too hot so i hope someone can help me. I need to select all the records from one table that do not exist in 2 other tables. I know it sounds simple enough but for some reason i can not get it working. It may have something to do with the fact that the field i am searching on are datetime fields. Here is a shortened version of my code.


SELECT DateOfStats
FROM table1
WHERE (DateOfStats NOT IN
(SELECT dateofstats
FROM table2)) and (DateOfStats NOT IN
(SELECT dateofstats
FROM table3))


Thanks for the help in advance.

View 1 Replies View Related

Selecting Only Those Records Which Are In Another Table

May 25, 2005

i need to select records from table "A" if only the "PK" of "A" exists in table "B". I need to return a resultset not just a single record. The problem is table "B" is not a table in database instead a user supplied table which can be a datatable in memory.

View 2 Replies View Related

Selecting Into A Generated Table Name. Can You Even Do This ?

Aug 22, 2001

Hello,

We're trying to do the following...

Declare @Name Varchar(15)

Select @Name = 'TableName'

Select @Name = @Name + Rand(SomeSeedValue)

Select *
Into @Name
From Someothertable

This just gives us syntax errors, anyone else had any success with this ?

View 1 Replies View Related

Create Table Selecting From Another

Mar 5, 2001

Hi!
I need to create a table and populate it with other table's contents. I know this can be done DTS, I wonder if there are means to make it throught a single TSQL command.
Thanks,
Fabio

View 1 Replies View Related

Selecting Last 100 Rows From A Table.

Jan 7, 2004

I have a temporary table that contain 1000 rows. I want to insert records from 900 to 1000 into another temporary table. Is ther any query that will do this in SQL server. With cursors I can do that but I need one shot query. Please give me some suggestions.

I using this query but it is not returning any rows.
select * into #t2 from #t1 t1 where not exists( select top 900 * from #t1 t2)

View 6 Replies View Related

Selecting Last 'n' Records From Table.

Nov 17, 2005

I was given this query but could not build it successfully.
can anybody help me out.

Consider the following scenario:

we have a table with single field of type Varchar(100).
we have few hundred records in it.
we want to retrieve last 'n' records from table.

i know this is bad idea but we need to have some solution to work it out.
i'm breaking my head but to no use.

Please help

View 3 Replies View Related

Selecting From A Dynamic Table Name

Sep 25, 2015

I've got an update statement with a subquery (I'll post the code further down) that I need to either make dynamic or do something else to make sure it does what I want.

The query is as follows:-
UPDATE dbo.tbl_Process_List_Control_Table
SET LastUpdateDateTime = (
SELECT ISNULL(MAX([LatestRowUpdateDateTime]), @LastUpdateDateTime)
FROM [wtbl_Process_List_Patient]
)
WHERE ProcessList = @ProcessName
This is a called proc with the following parameters:-
@LastUpdateDateTime is the date of the last record to be loaded
@ProcessName is the name of the process that was started/finished.

I need to make [wtbl_Process_List_Patient] dynamic so it looks at a different table based on a passed parameter.I've tried making the whole thing dynamic but it states I need to declare @LastUpdateDateTime which I can't see how to do as it's already passed to the proc (as is the process name).

View 6 Replies View Related

Selecting From A Linked Table

Jun 20, 2008

Hi

I've created a linked table connection to an external data source within sql server 2005.

The link looks OK, I can see all the tables etc, but I want to create a view on another database to some of the tables on the linked database.

How do I perform something as simple as a select statement as I can't seem to be able to select anything.

Is there something that i need to do first in order to be able to select fields from the linked table?

Thanks

View 3 Replies View Related

Selecting Variable As Table

Jul 5, 2007

Good Day guys, sorry i'm just new in SQL Server.

My problem is like this:

I want to pass Table Object to a declared variable and make View.

Usually we make like this:

"Select * from Table1"

I want like this:

"Select * from @Table1"

I want to pass Table1 to a variable "@Table1" which the variable
reads as Table.

Please help me guys, i really appriciated everything.

Thanks,

Ero-Sennin26

View 5 Replies View Related

Selecting Rows From One Table That Are Not In The Other

Apr 20, 2006

hi!I have two tables with same structure. I need to run a select query thatwill return only the rowsfrom one table that are not in the other.In MySQL it would beselect * from table1 MINUS select * from table2.but in MsSql I can not find the apropriate way to do this.Any Help?Zvonko

View 3 Replies View Related

Dynamically Selecting Table In DTS

Sep 1, 2006

Hello,

I have to design a DTS package (not SSIS ) in which i want to select the destination table dynamically. Can any one help me out.

Thanks

MV

View 2 Replies View Related

Selecting The Most Recently Edited Item AND Selecting A Certain Type If Another Doesn't Exist

Sep 20, 2007

I've got a big problem that I'm trying to figure out:
I have an address table out-of-which I am trying to select mailing addresses for companies UNLESS a mailing address doesn't exist; then I want to select the physical addresses for that company. If I get multiple mailing or physical addresses returned I only want the most recently edited out of those.

I don't need this for an individual ID select, I need it applied to every record from the table.

My address table has some columns that look like:
[AddressID] [int]
[LocationID] [int]

[Type] [nvarchar](10)
[Address] [varchar](50)
[City] [varchar](50)
[State] [char](2)
[Zip] [varchar](5)
[AddDate] [datetime]
[EditDate] [datetime]

AddressID is a primary-key non-null column to the address table and the LocationID is a foreign key value from a seperate Companies table.
So there will be multiple addresses to one LocationID, but each address will have it's own AddressID.

How can I do this efficiently with perfomance in mind???

Thank you in advance for any and all replies...

View 2 Replies View Related

Selecting From Table On Cond In A Another Table

Apr 16, 2008

I have table HITS with columns ID and hitDate and table DATESS with columns startDate and endDate.
I can have alot of entries in the HITS table with hitDate between the times of a startDate and endDate entry in the DATESS table.
What I am trying to do is to select all the ID's of the entries where hitDate is between startDate and endDate. (In the end I would use min and max to get the firt and last hit between those times)

I have tried:
SELECT ID from hits,Datess WHERE hitdate between startDate and endDate

but it does not give the required results. I think the problem is the tables are not related in any way. Is there another way to do this.


Thanks

View 2 Replies View Related

How To Create Index On Table Variable (Table Don't Have Primary Key)

Feb 26, 2008



Hi all,


my stored procedure have one table variable (@t_Replenishment_Rpt).I want to create an Index on this table variable.please advise any of them in this loop...
below is my table variable and I need to create 3 indexes on this...


DECLARE @t_Replenishment_Rpt TABLE
(
Item_Nbr varchar(25) NULL,
Item_Desc varchar(255) NULL,
Trx_Date datetime NULL,
Balance int NULL,
Trx_Type char(10) NULL,
Issue_Type char(10) NULL,
Location char(25) NULL,
Min_Stock int NULL,
Order_Qty int NULL,
Unit char(10) NULL,
Issue_Qty int NULL,
Vendor varchar(10) NULL,
WO_Nbr varchar(10) NULL,
Lead_Time int NULL,
PO_Nbr char(10) NULL,
PO_Status char(10) NULL,
Currency char(10) NULL,
Last_Cost money NULL,
Dept_No varchar(20) NULL,
MSDSNbr varchar(10) NULL,
VendorName varchar(50) NULL,
Reviewed varchar(20) NULL
)

I tryed all below senarios...it is giving error...


--Indexing the @t_Replenishment_Rpt table on the column Names Item Number, Vender , Department Number
--EXEC sp_executesql(CREATE UNIQUE CLUSTERED INDEX Replenishment_index ON @t_Replenishment_Rpt (Item_Nbr))
--CREATE UNIQUE CLUSTERED INDEX Idx1 ON @t_Replenishment_Rpt.Item_Nbr
INDEX_COL ( '@t_Replenishment_Rpt' , ind_Replenishment_id , Item_Nbr )
--EXEC sp_executesql('SELECT INDEXPROPERTY('+ '@t_Replenishment_Rpt' + ', ' + 'Item_Nbr' + ',' + 'IsPadIndex' + ')')
--EXEC sp_executesql(SELECT INDEXPROPERTY('@t_Replenishment_Rpt', 'Vendor','IsPadIndex'))
--EXEC sp_executesql(SELECT INDEXPROPERTY('@t_Replenishment_Rpt', 'Dept_No','IsPadIndex'))


View 3 Replies View Related

Selecting Exactly 1 Row But More Columns From A Related Table..

Jan 4, 2008

I have table 1 from which I select some values belonging to users.In table 2 I store tips a user might give: tipid, tiptitle, tiptext,tipcreatedateA user may give more than one tip.But now I want a query that selects the info of a SINGLE user and the LATEST tip he created, so resultset might look like:username lastname sex tiptitle tiptext tipcreatedateSo even though a user might have given more tips, only the latest will be retreived...and thus 1 row for a particular user is returned...How would I construct such a query? The problem is that I want to get exactly 1 row but more than 1 column from the table 2, so I think I cannot use the SQL Server "TOP" command...

View 6 Replies View Related

Selecting Multiple Values From One Table

Apr 29, 2008

I have a sql select query that I'm pulling from a "Years" table to link to 3 columns in an Items table.ZCValuesYear table has two colums: YearID and YearYearID        Year1            20042            20053            20064            20075            2008...I want to bind the "Year" value to the three colums in the ZCItem table:     ItemUseFirstYearID     ItemUseLastYearID     ItemYearIDThe query below will pull all the "ID's" for each of the colums, but how would I make it pull the "Year" value (instead of record 4, it would pull 2007 instead)?<asp:SqlDataSource ID="sqlItemSelect" runat="server"         ConnectionString="<%$ ConnectionStrings:MyConnString %>" SelectCommand="SELECT ZCPartVault.PartVaultID, ZCPartVault.PartVaultItemID, ZCValuesYear.Year, ZCItem.ItemName, ZCItem.ItemUseFirstYearID, ZCItem.ItemUseLastYearID         FROM ZCPartVault     FULL OUTER JOIN ZCItem ON ZCPartVault.PartVaultItemID = ZCItem.ItemID     FULL OUTER JOIN ZCValuesYear ON ZCItem.ItemUseLastYearID = ZCValuesYear.YearID AND ZCItem.ItemUseFirstYearID = ZCValuesYear.YearID AND ZCItem.ItemYearID = ZCValuesYear.YearID" >        </asp:SqlDataSource>       

View 2 Replies View Related

SELECTing Random Record From TABLE?

Apr 24, 2001

Hello.
I need to select a random record from TABLE. It might look easy with using RAND() function, but the tricky part is that ID's which are the PRIMARY KEY, were assigned as a random number.
So right now ID's in that TABLE look some thing like that: -18745, 45809, 129, -5890023, 487910943, -209, etc...
If any one have any ideas please respond.
Thanks in advance.

View 2 Replies View Related

Selecting Table Names In A SQL Statement

Jun 12, 2001

Hey all,

I trying to figure out if you can get a list of table names from a particular database by writing a single SQL statement.

E.G. In Oracle, from SQLPlus - you can say:

Select table_name from user_tables;

Is there anything like that SQL that I could run from Query Analyser to get a list of table names from a particular SQL Server database?

Thanks in advance,
Tommy

View 3 Replies View Related

Selecting Rows By There Position In The Table

Jan 25, 2001

Hy!

I'm trying to write a query which returns only some rows in my table...

For example :

I want all the rows included between 30 and 50 from table XYZ
(30 and 50 are not keys but really the numbers of records)

Is there someone out there who has a hint for me???


Thank you for your help and sorry for my english!

RadiFluide

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved