I have a table called ORG_MAP, it contained 200 records, I executed a query to copy those 200 records to other table called ORG_MAP_1 (duplicate table of ORG_MAP), but what happened was the same 200 records got inserted into the same ORG_MAP table.
Now I want to remove those duplicated records. I don't know how to do that.
Please help me. I'm using db visualizer8.0.11 version.
the query I used was:
INSERT INTO METRICS.ORG_MAP (REPORT_END_DATE,ENTITY,ENTITY_TYPE,RELATIONSHIP,TARGET,TARGET_TYPE)
SELECT REPORT_END_DATE,ENTITY,ENTITY_TYPE,RELATIONSHIP,TARGET,TARGET_TYPE
FROM METRICS.ORG_MAP
i dont think there is any direct solution for your problem
but i have done some work around may be useful for you....
create table #table(id int,c varchar(2))
insert into #table values('1','a')
insert into #table values('1','a')
insert into #table values('2','b')
insert into #table values('2','b')
insert into #table values('3','c')
insert into #table values('3','c')
insert into #table values('4','d')
insert into #table values('5','d')
declare #table table(uid int,id int ,c varchar(2));
with tbl as (
select ROW_NUMBER() OVER(ORDER BY id DESC) AS uid ,id,c from #table
)
insert into #table select * from tbl
declare #tmpid int,#tmpuid int,#cnt int
declare tmpc cursor
for select uid,id from #table;
open tmpc
fetch next from tmpc
into #tmpuid,#tmpid
while ##FETCH_STATUS = 0
BEGIN
set #cnt = (select COUNT(id) from #table where id = #tmpid)
IF (#cnt > 1)
BEGIN
delete from #table where uid = #tmpuid
END
fetch next from tmpc
into #tmpuid,#tmpid
END
CLOSE tmpc;
DEALLOCATE tmpc;
select * from #table
drop table #table
Related
Having the following procedure:
CREATE PROCEDURE [dbo].[Gest_Doc_SampleProc]
#Nome nvarchar(255),
#Descritivo nvarchar(255),
#SampleTable AS dbo.IDList READONLY
AS
DECLARE #foo int;
SELECT #foo=a.bar FROM TableA a WHERE a.Nome=#Nome
IF NOT EXISTS (SELECT a.bar FROM TableA a WHERE a.Nome=#Nome)
BEGIN
INSERT INTO TableA VALUES (#Nome,#Descritivo)
INSERT INTO TableB VALUES (scope_identity(),#SampleTable)
END
I am trying, as shown, inserting into TableB all the values of SampleTable, together with the scope_identity.
SampleTable is as:
CREATE TYPE dbo.SampleTable
AS TABLE
(
ID INT
);
GO
How can I correctly achieve this?
The right way to do this type of work is the OUTPUT clause. Although technically not needed for a single row insert, you might as well learn how to do it correctly. And even what looks like a single row insert can have an insert trigger that does unexpected things.
PROCEDURE [dbo].[Gest_Doc_SampleProc] (
#Nome nvarchar(255),
#Descritivo nvarchar(255),
#SampleTable AS dbo.IDList
) READONLY AS
BEGIN
DECLARE #ids TABLE (id int);
DECLARE #foo int;
SELECT #foo = a.bar
FROM TableA a
WHERE a.Nome = #Nome;
IF NOT EXISTS (SELECT 1 FROM TableA a WHERE a.Nome = #Nome)
BEGIN
INSERT INTO TableA (Nome, Descritive)
OUTPUT Inserted.id -- or whatever the id is called
INTO #ids;
VALUES (#Nome,#Descritivo)
INSERT INTO TableB (id, sampletable)
SELECT id, #SampleTable
FROM #ids;
END;
END; -- Gest_Doc_SampleProc
In addition to using OUTPUT, this code also adds column lists to the INSERTs. That is another best practice.
For table1 Inserted 3 records
It should get those three identities and it should insert 3 records in table3 (but it’s not happening- it inserts 3 records with same identity ie.last scope identity)
create table table1(ID INT identity(1,1),Name varchar(50))
insert into table1 values('Ram'),('Sitha'),('Laxman')
create table table1(ID INT identity(1,1),Name varchar(50))
create table table3(ID INT ,Name varchar(50))
insert into table2(Name)
select Name from table1
declare #id int;
set #id= (select scope_Identity())
begin
insert into table3(ID,Name)
select #id,Name from table2
end
select * from table2
select * from table3
How can get all identities to insert do I need to write a loop (or) do I need to Create a trigger.
Please give me a solution I am strugguling from past 4 hours.
Thanks in anvance
Use the OUTPUT clause to handle multi-row inserts:
INSERT INTO dbo.table2(Name)
OUTPUT inserted.ID, inserted.Name INTO table3
SELECT Name FROM dbo.table1;
You can use the OUTPUT clause to get the identity from any number of inserts.
create table table1(ID INT identity(1,1),Name varchar(50))
DECLARE #T1 Table (ID int, name varchar(50))
insert into table1
OUTPUT inserted.ID, Inserted.Name INTO #T1
values('Ram'),('Sitha'),('Laxman')
DECLARE #IdentityId INT,#Count INT=1
DECLARE #temp AS TABLE (Id INT IDENTITY ,Name NVARCHAR(100))
INSERT INTO #temp(Name)
SELECT Name FROM table1
WHILE #Count <=(SELECT COUNT(SId) FROM #temp)
BEGIN
INSERT INTO table2(Name)
SELECT Name FROM #temp
WHERE Id=#Count
SET #IdentityId = SCOPE_IDENTITY()
INSERT INTO tabel3(#IdentityId,Name)
SELECT 3, #IdentityId,1,GETDATE()
SET #Count=#Count+1
END
DECLARE #ID INT
SET #ID = (select top 1 USER_REQ_JOB_ID
from T8504_USER_REQ_JOB
where JOB_GRP_ID = 160
order by LST_UPDT_TS desc)
SELECT INPUT_PARM_VAL_TX
from TBL_RPT_JOB_INPUT_PARAM
where USER_REQ_JOB_ID = #ID
This returns these results:
USA
USCC
6
7
2
These five records what I get I want to assign to five different variables to use in stored procedure.
I was trying with table variable like this :
declare #CID table (
Region Char(3)
,Segment Char(3)
,MasterContractId int
,ctcid int
,templateid int)
insert into #CID (Region,Segment,MasterContractId,ctcid,templateid)
But how to insert that 5 rows here?
INSERT INTO #CID
select * from
(
select
'Temp' + convert(char(1), row_number() over (order by (select 0))) as columnName,
INPUT_PARM_VAL_TX as Value
from TBL_RPT_JOB_INPUT_PARAM where USER_REQ_JOB_ID = #ID
) d
pivot
(
max(value)
for columnname in (Temp1, Temp2, Temp3, Temp4, Temp5)
) piv;
See if this helps.
Take a look at this fiddle for an example.
Courtesy:
Add row number to this T-SQL query
Efficiently convert rows to columns in sql server
EDIT: The sql adds an extra column to generate row numbers to use it as an extra column, which is pivoted as column heading.
it's really gross, but one way you could probably do it is this (though you'll need to apply it to your case):
http://sqlfiddle.com/#!6/d41d8/21507
declare #table TABLE (value varchar(50))
INSERT INTO #table
VALUES ('first')
INSERT INTO #table
VALUES ('second')
INSERT INTO #table
VALUES (3)
INSERT INTO #table
VALUES (4)
DECLARE #temp TABLE (id int identity(1,1), value varchar(50))
INSERT INTO #temp
SELECT [value]
FROM #table t
SELECT *
FROM #temp
DECLARE #CID TABLE (Region varchar(50), cont varchar(50), another int, andAnother int)
INSERT INTO #CID
(
Region,
cont,
another,
andAnother
)
VALUES
(
(SELECT value FROM #temp WHERE id = 1), -- Region - varchar
(SELECT value FROM #temp WHERE id = 2), -- cont - varchar
(SELECT value FROM #temp WHERE id = 3), -- another - int
(SELECT value FROM #temp WHERE id = 4) -- andAnother - int
)
SELECT * FROM #cid
note that i assumed you're using mssql, you did not specify
I'd like to copy values from TableA to TableB using cursor but getting troubles all the time. Here is my code:
DROP TABLE #Firmy
DROP TABLE #Faktury
CREATE TABLE #Firmy (
idfirmy VARCHAR(255)
)
INSERT INTO #Firmy VALUES ('00001936')
INSERT INTO #Firmy VALUES ('00002059')
CREATE TABLE #Faktury (
idfirmy VARCHAR(255)--,
--idfaktury VARCHAR(255)
)
DECLARE fakturyc CURSOR FOR SELECT * FROM #Firmy
FOR loop1 AS fakturyc
DO
INSERT INTO #Faktury VALUES fakturyc
END FOR
Getting "Syntax error near loop1". Can someone help me with solving this? Thanks :)
This is actually T-sql: but may work in sybase as well
DROP TABLE #Firmy
DROP TABLE #Faktury
CREATE TABLE #Firmy (
idfirmy VARCHAR(255)
)
INSERT INTO #Firmy VALUES ('00001936')
INSERT INTO #Firmy VALUES ('00002059')
CREATE TABLE #Faktury (
idfirmy VARCHAR(255)--,
--idfaktury VARCHAR(255)
)
declare #col1 varchar(10)
declare copy cursor for
select idfirmy from #Firmy -- select top 1 * from #Firmy
open copy
fetch next from copy into #col1
while ##FETCH_STATUS=0
begin
insert into #Faktury(idfirmy) values(#col1)
fetch next from copy into #col1
end
close copy
deallocate copy
select * from #Faktury
select * from #Firmy
The simplified Query will be
insert into #Faktury
select * from #Firmy
question for sql server query.
I try to create a query for searching every word on a sendence. words are split by a space.
The result contain lines that the value contain every word not at least 1.
sample:
DECLARE #DATATABLE TABLE (ID INT,COL1 VARCHAR(MAX))
INSERT INTO #DATATABLE (ID,COL1) VALUES (1,'ALPHA')
INSERT INTO #DATATABLE (ID,COL1) VALUES (2,'ALPHA BETA')
INSERT INTO #DATATABLE (ID,COL1) VALUES (3,'ALPHA BETA CHARLIE')
INSERT INTO #DATATABLE (ID,COL1) VALUES (4,'ALPHA BETA CHARLIE DELTA')
DECLARE #TESTTABLE AS TABLE (ID INT,COL1 VARCHAR(MAX))
INSERT INTO #TESTTABLE (ID,COL1) VALUES (1,'ALPHA') -- RETURN 1,2,3,4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (2,'ALPHA BETA') -- RETURN 2,3,4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (3,'BETA CHARLIE ALPHA') -- RETURN 3,4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (4,'BETA DELTA') -- RETURN 4
INSERT INTO #TESTTABLE (ID,COL1) VALUES (5,'CHARLIE') -- RETURN 3,4
SELECT * FROM #DATATABLE
DECLARE #I AS INT
SET #I = 1
DECLARE #COUNTLINE AS INT
SET #COUNTLINE = (SELECT MAX(ID) FROM #TESTTABLE )
DECLARE #CURRENTVALUE AS VARCHAR(MAX)
WHILE(#I <= #COUNTLINE)
BEGIN
SET #CURRENTVALUE = (SELECT COL1 FROM #DATATABLE WHERE ID = #I)
-- ** TODO **
-- QUERY SHOULD RETURN EVERY LINE CONTAINING EACH WORD OF THE CURRENT TEST VALUE
SELECT * FROM #DATATABLE WHERE COL1 LIKE #CURRENTVALUE
SET #I = #I + 1
END
I write "TODO" where I put the first test (saw what that shouldn't do).
I cant create SP or Function.
I look on my side for "with" and "in" or something else.
tank for any tips
and good week end