|
I have a SQL question if any of you have time:
I want to do an update using two tables. I used to know how to do this but I've forgotten.
Put simply I havw two tables, Cars and UnMatched
Cars:
ModelCode number (Primary Key)
FuelType text
UnMatched:
ID number (Primary Key)
FuelType text
Cars has lots of rows, Unmatched has less.
I want to update Cars setting the FuelType to the value of FuelType in Unmatched, where the ModelCode in Cars = the ID in UnMatched.
So for example if Cars had the following Rows:
10 ""
11 ""
12 ""
13 ""
And UnMatched had these rows
11 "PETROL"
13 "DIESEL"
14 "HYBRID"
You'd end up with Cars being
10 ""
11 "PETROL"
12 ""
13 "DIESEL"
I know it's some kind of correlated subquery but I can't remember what it is.
This is on an Access database but I'd imagine it would be the same on others.
Thanks - Ben
|
|
|
|
|
In SQL Server you would do
UPDATE Cars
SET FuelType = UnMatched.FuelType
FROM UnMatched
WHERE ModelCode = UnMatched.ID It acts rather like a join but you don't qualify the names of columns for the table you're updating with the name of the table.
However, I don't think this is supported by Jet.
DoEvents: Generating unexpected recursion since 1991
|
|
|
|
|
Hi all
i want to convert char data to datetime but it dosent give me reqd output
select convert(char(8),fieldname,108) as myTime from TableName
fieldname datatype is char(8) it contain values like 1004,1200,1354
required outpot as 10:04,12:00,13:54
i used above expression but it doesn`t convert data in hh:mm:ss
plz help
The Stifler
--
Bugs can neither be created nor be removed from software by a developer. They can only be converted from one form to another. The total number of bugs in the software always remain constant.
|
|
|
|
|
This will show the result in the required format, but it does not convert it to datetime:
Select substring(fieldname,1,2) +':'+ substring(fieldname,3,2)
|
|
|
|
|
Hi Friends,
How are you,i have one doubt,i made a wrong update to my table and now i want to rollback the transcation,i have used sql query analyser.
Kindly please tell me the solution to rollback.
Thanks & Regards
Senthil Kumar
|
|
|
|
|
Do you have any backups before the change?
If no, I'm afraid this won't be possible.
|
|
|
|
|
You're misunderstanding what a transaction is.
You can only rollback the transaction if the transaction hasn't been commited yet. If you used the Query Analyer to make this change using an UPDATE query, it's done. The transaction was started, the change was made, and the transation automatically committed when you clicked on the Execute button.
AFAIK, there is no way to roll this back without restoring the database from backup.
|
|
|
|
|
I have to tables MASTER_ACCOUNTS and DEPOT_ACCOUNTS. I need to UPDATE the MASTER_ACCOUNTS table with the values in DEPOT_ACCOUNTS where the ACCOUNTID columns match. Both table structures are the same.
Not sure how I should achieve this, so any help would be appreicated.
Thanks
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|
|
The long way
update master_accounts
set master_accounts.a = de.a,master_accounts.b = de.b,etc...
from master_accounts
join depot_accounts de
on master_accounts.acountid = de.accountid
Or if you are feeling like throwing caution to the wind:
Another thing you can do is delete all the rows in master_accounts that have a matching accountid in depot_accounts.
Keep a record of these accountids in a temp table first.
Then insert all the rows in depot_accounts that correspond, to the accountids in the temp table, into master_accounts.
Since the table structures are the same this will mean that you do not need to type all those = statements.
You always pass failure on the way to success.
|
|
|
|
|
I was hoping there would be a better, more eligent way, like the INSERT INTO aTable SELECT * FROM bTable .
Ho well! Thanks for educating me.
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|
|
You would have the issue of duplicated records if you ran the query this way.
Instead of insert you would have to use update and this requires column names to be specified.
This is because insert does not replace records it appends records.
Although SQL is a declarative language you still need to do a lot of work in writing the queries
Microsoft are apparently developing new declarative languages so in the future things may be easier...
Regards
Guy
You always pass failure on the way to success.
|
|
|
|
|
Hi I need to write a function in SQL that returns the date of the previous Monday, for example:
if today is Tuesday thru Sunday then get previous Monday
else if today is Monday get todays date.
Any help would be appreciated.
Thanks.
|
|
|
|
|
I actually got this to work so I thot I'd share...
ALTER FUNCTION [dbo].[fn_GetMonday]
( @pInputDate DATETIME )
RETURNS DATETIME
BEGIN
DECLARE @Result DATETIME
SET @pInputDate = CONVERT(VARCHAR(10), @pInputDate, 111)
IF DATENAME(dw, @pInputDate) = 'Sunday'
BEGIN
SET @Result = DATEADD(DD, -6, @pInputDate)
END
ELSE
SET @Result = DATEADD(DD, -DATEPART(DW, @pInputDate) + 2, @pInputDate)
RETURN @Result
END
|
|
|
|
|
I am no expert and there may be a better way of doing it, but have a look at DATEPART and DATEADD functions along with a WHILE loop.
DECLARE @MyDate DATETIME<br />
SET @MyDate = GETDATE()<br />
WHILE (DATEPART(dw, @MyDate)) <> 'Monday'<br />
BEGIN<br />
SET @MyDate = DATEADD(day, -1, @MyDate)<br />
END
MyDate should now contain the date of the previous monday or today's date if today is a Monday.
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|
|
I have an application in which I fill a listbox with values from a dataset. But the listbox is filled with 'system.data.datarowview' here the values are displayed correctly.But when the page is load for the 1st time tha list box showing 'system.data.datarowview'after click on dat dat coming..plz help me
i ve writen the code like
con.Open();
SqlDataAdapter adp= new SqlDataAdapter(lstrsql,con);
DataSet objds=new DataSet();
adp.Fill(objds);
lst_emp.DataTextField=objds.Tables[0].Columns["txtval"].ToString();
lst_emp.DataValueField=objds.Tables[0].Columns["emp_id"].ToString();
lst_emp.DataSource = objds;
lst_emp.DataBind();
|
|
|
|
|
Wrong forum, try posting your question in the C# forum. Also it would be better if you provided a descriptive subject line, rather than 'Hi' or 'Hello' or 'Urgent' etc.
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|
|
Hi....
you need to change the below lines...
mon***z wrote: lst_emp.DataTextField=objds.Tables[0].Columns["txtval"].ToString();
lst_emp.DataValueField=objds.Tables[0].Columns["emp_id"].ToString();
to......
lst_emp.DataTextField = "txtval";<br />
lst_emp.DataValueField = "emp_id";
Regards,
Sandeep Kumar.V
|
|
|
|
|
con.Open();<br />
<br />
SqlDataAdapter adp= new SqlDataAdapter(lstrsql,con);<br />
DataSet objds=new DataSet();<br />
adp.Fill(objds); <br />
<br />
lst_emp.DataSource = objds.Tables[0].DefaultView<br />
cmbBookFrom.ValueMember = "emp_id"<br />
cmbBookFrom.DisplayMember = "txtval"
i think this will make your life easier
|
|
|
|
|
I have the following INSERT command which works well, but I need to limit what is inserted.
INSERT INTO SalesLogix.sysdba.Account SELECT * FROM DONCASTER4.SalesLogix.sysdba.Account
FYI: DONCASTER4 is a linked server.
What I need to do is only insert records where the value of the AccountId column of the source table does not already exist in the destination table.
Both tables are identical and both servers are MS SQL Server 2005.
Your help on this is appreicated.
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
modified on Wednesday, February 20, 2008 5:45 AM
|
|
|
|
|
User IF NOT EXISTS
for example
IF NOT EXISTS (SELECT * FROM TblA Where AcctId = <<i>value>) INSERT INTO TblA .....
Regards
KP
|
|
|
|
|
Use IF NOT EXISTS
for example ...
IF NOT EXISTS (SELECT * FROM TblA WHERE AcctId = <<i>value>) INSERT INTO TblA .....
Regards
KP
|
|
|
|
|
Thanks for the reply KP, but I do not think you understood my problem which is I have a remote SQL Server (DONCASTER4) on to which new records are inserted into the ACCOUNT table.
Every evening I wish to make a copy of records added to the Accounts table on the DONCASTER4 server onto my master database.
Hope this clarifies what I am trying to achieve.
Regards
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|
|
I've got it :-
INSERT INTO SalesLogix.sysdba.ACCOUNT <br />
SELECT * <br />
FROM Doncaster4.salesLogix.sysdba.ACCOUNT AS DONCASTER<br />
WHERE NOT EXISTS(SELECT * FROM SalesLogix.sysdba.ACCOUNT AS HARTSHILL WHERE DONCASTER.ACCOUNTID = HARTSHILL.ACCOUNTID)
Thanks for your help
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|
|
One of our project is a content management system something like wikipedia. So I need to calculate the visitors count getting for each article. Each article in this will have a unique id. So I am looking for a best method to keep the visitor count in database.
I have a master table where article details are kept. I tried adding a column named "PageView" which will be updated each time the page is requested. This worked well when website gets very less traffic. But shows incorrect results when it's running on heavy traffic. I guess it's a row level locking issue. Is there any other better approach which can be used to keep the accurate visitor count ?
The work around which is in my mind is, putting a lock to the row when I select "PageView" for updating. Update the count and release the lock. If this is right, will the other select requests to the same row wait until the lock get released ?
|
|
|
|
|
N a v a n e e t h wrote: putting a lock to the row when I select "PageView" for updating. Update the count and release the lock
Why do you select the row and lock it? why don't you just execute a simple update ? also note that in most cases, sql server will handle locks in the best way.
can you post the code you use?
|
|
|
|