2016-11-28 64 views
1
create table tbl1(rno int, name varchar(10)) 
insert into tbl1 values(101, 'neha') 

alter table tbl1 add city varchar(10) 
select * from tbl1 

在此代碼中,我將記錄插入城市列。我也嘗試了下面的代碼,但是這不正確的代碼需要幫助來添加記錄。如何在使用SQL Server的現有列中插入記錄

insert into tbl1 (city) 
    SELECT CITY 
    FROM tbl1 
    WHERE rno = 1 

update tbl1 
set city = 'pune' 
where rno = 1; 

第2個查詢返回「0記錄更新」答案。

+0

從列做您要更新的城市?它在同一張桌子嗎? – Bharat

+0

是否有找到rno = 1的任何記錄?我認爲沒有找到記錄,所以更新不起作用... –

+0

第二個查詢沒有更新任何內容,因爲你表中的行有'rno = 101' - 不是'rno = 1' ...... –

回答

1

您插入到表中該行有rno = 101 - 所以你UPDATE語句必須是這樣的:

update tbl1 
set city = 'pune' 
where rno = 101; -- use **101** here - not **1** !! 
相關問題