I am using SQL server 2008. I want to insert data from a temp table to database. I am using While loop to insert data from temp table to database table.
Now I am facing an issue:
object already exist in the database.
declare #rev as int ,
#sQuotationNo NVARCHAR(15),
#sQRevNo int
set #rev=(select top 1 QRevNo from PDBCompr Where QuotationNo='JCS_G1415_008' and QRevNo<>'3' order by QRevNo desc)
;with cte as
(
SELECT
ROW_NUMBER() OVER(ORDER BY QuotationNo) AS sSLNO,
[CompanyCode] ,
[ProjectCode] ,
[PRevNo],
[CSlNo],
[ComprDescription] ,
[PID] ,
[RatingCode] ,
[Rating] ,
[StdSystems] ,
[BoosterSystems] ,
[GCUSystems] ,
[KOFSystems] ,
[HeaterSystems] ,
[OtherSystems] ,
[Comments] ,
[Currency1] ,
[UnitPrice1] ,
[Currency2] ,
[ExchRate2] ,
[UnitPrice2] ,
[Currency3],
[ExchRate3] ,
[UnitPrice3] ,
[CreateId] ,
[CreateDate] ,
[UpdateId] ,
[UpdateDate]
from PDBCompr
where QuotationNo='JCS_G1415_008' and CompanyCode ='001' and QRevNo ='2'
and AddCmprId not in (select distinct AddCmprId from PDBCompr where QuotationNo='JCS_G1415_008' and CompanyCode ='001' and QRevNo ='3' )
)
select * into #temp from cte
declare #cnt int , #loopCnt int=1
select #cnt =( select COUNT(*) from #temp)
while (#loopCnt<=#cnt)
begin
;with cte2 as
(
SELECT
sSLNO,
[CompanyCode] ,
[ProjectCode] ,
(select Max(PRevNo)+1 from PDBCompr) [PRevNo],
(select Max(CSlNo) +1 from PDBCompr)[CSlNo],
[ComprDescription] ,
[PID] ,
[RatingCode] ,
[Rating] ,
[StdSystems] ,
[BoosterSystems] ,
[GCUSystems] ,
[KOFSystems] ,
[HeaterSystems] ,
[OtherSystems] ,
[Comments] ,
[Currency1] ,
[UnitPrice1] ,
[Currency2] ,
[ExchRate2] ,
[UnitPrice2] ,
[Currency3],
[ExchRate3] ,
[UnitPrice3] ,
[CreateId] ,
[CreateDate] ,
[UpdateId] ,
[UpdateDate]
from #temp where sSLNO=#loopCnt
)
select * into PDBCompr from cte2
set #loopCnt= #loopCnt+1
drop table #temp
end
Please help me to find a proper solution. Thanks in advance
select * into PDBCompr from cte2
in this line you are again making a table "PDBCompr", but in above code you are selecting records from this table it means its already exist.
so if you want to insert record in this table then you need to change your query like below:
insert into PDBCompr select * from cte2
Related
I am new to this and i am wondering how to input data into SSRS table and auto generate for the subsequent months. This is the format of the table.
Appreciated for any help given.
You can generate a date range using the following SQL
DECLARE #date_start AS DATETIME
SET #date_start = '01-DEC-2017'
;WITH
finalvalues
AS
(
SELECT tbl.* FROM (VALUES
( '01-Dec-2017', 6414.6563, 429.6846, -1390.8474)
, ( '02-Dec-2017', 6476.6563, 432.751, -1312.4928)
, ( '03-Dec-2017', 6538.6563, 435.8174, -1234.1382)
, ( '04-Dec-2017', 6600.6563, 438.8838, -1155.7836)
, ( '05-Dec-2017', 6662.6563, 441.9502, -1077.429)
, ( '06-Dec-2017', 6724.6563, 445.0166, -999.074399999999)
, ( '07-Dec-2017', 6786.6563, 448.083, -920.719799999999)
, ( '08-Dec-2017', 6848.6563, 451.1494, -842.365199999999)
, ( '09-Dec-2017', 6910.6563, 454.2158, -764.010599999999)
, ( '10-Dec-2017', 6972.6563, 457.2822, -685.655999999999)
, ( '11-Dec-2017', 7034.6563, 460.3486, -607.301399999999)
, ( '12-Dec-2017', 7096.6563, 463.415, -528.946799999999)
, ( '13-Dec-2017', 7158.6563, 466.4814, -450.592199999999)
, ( '14-Dec-2017', 7220.6563, 469.5478, -372.2376)
, ( '15-Dec-2017', 7282.6563, 472.6142, -293.883)
, ( '16-Dec-2017', 7344.6563, 475.6806, -215.5284)
) tbl ([Date], [IncLoad], [ITLoad], [RH])
)
,
manufactured_dates
AS
(
SELECT
day_date = DATEADD(day, dte.[number], #date_start)
FROM
master.dbo.spt_values AS dte
WHERE
1=1 -- <-- used in testing to be able to comment out other clauses below
AND dte.[type] = 'P'
AND dte.[number] <= 365 -- <-- filter how many rows you want to see here
)
SELECT
'Date' = md.[day_date]
, 'IncLoad' = AVG(incload)
, 'ITLoad' = AVG(ITLoad)
FROM
finalvalues AS fv
FULL OUTER JOIN manufactured_dates AS md ON md.[day_date] = fv.[Date]
GROUP BY
md.[day_date]
The following MySQL query results sum of credit and debit for each account code (acctcode) and produces over all total using union all. The table ledg_post has 5.7 million records and is indexed. Still the query takes 1 minute to execute. Please help me to tune this query.
select b.acnt_code as acctcode
, b.disp_name as acctname
, sum(amt_dr) as debit
, sum(amt_cr) as credit
, (sum(amt_dr)- sum(amt_cr)) as closingbalance
, a.txn_code as txn_code
from ledg_post a
, gl_acnts b
, mst_loan lmt
where a.acnt_code = b.acnt_code
group
by b.acnt_code
union all
select ' ' acctcode
, ' Grand Total ' acctname
, sum(amt_dr) debit
, sum(amt_cr) credit
, (sum(amt_dr)- sum(amt_cr)) closingbalance
, '' txn_code
from ledg_post a
, mst_loan lmt
where lmt.loan_id = a.ref_id
Table Definitions
create table ledg_post
( txn_code int(11)
, ref_id int(11)
, acnt_code int(11)
, amt_dr decimal(20, 2)
, amt_cr decimal(20, 2)
);
create table gl_acnts
( glm_acnt_code int
, glm_acnt_disp_name varchar(50)
);
create table mst_loan
( lmt_loan_id int(11)
, lmt_clnt_id int(11)
);
There is no need to use UNION ALL, You can achieve this using GROUP BY ... WITH ROLLUP
Try this:
SELECT b.acnt_code AS acctcode, IFNULL(b.disp_name, ' Grand Total ') AS acctname,
SUM(a.amt_dr) AS debit, SUM(a.amt_cr) AS credit,
(SUM(a.amt_dr)- SUM(a.amt_cr)) AS closingbalance,
IFNULL(a.txn_code, '') AS txn_code
FROM ledg_post a
INNER JOIN gl_acnts b ON a.acnt_code=b.acnt_code
GROUP BY acctcode WITH ROLLUP
The webguys wants unique urls based on the name of the products
If more products have the same name, add a number after the name.
our.dom/red-sock
our.dom/red-sock-1
They do not want the product id or another number on all products, i.e.
our.dom/red-sock-123481354
I store this in a field i call seourl.
I have it covered when I create new products, a trigger tries adding the seourl, if it is already there, increment the number, until an unique value is found.
But I now have to give the entire table new seourls.
If I just
update tab set seourl=dbo.createurl(title)
I am sure to have collissions, and the operation is rolled back.
Is there a way to have the statement to commit the updates that work, and leave the rest unchanged?
Or must I just do a RBAR, Row By Agonizing Row operation in a loop?
Adapt this to your needs:
select
*
from (values('aaa'), ('aaa-12'), ('aaa-'), ('bbb-3')) as src (x)
cross apply (
select isnull(nullif(patindex('%-[0-9]%', x) - 1, -1), LEN(x))
) as p(idx)
cross apply (
select
SUBSTRING(x, 1, idx)
, SUBSTRING(x, idx + 1, LEN(x) - idx)
) as t(t, xx)
Try this:
declare #tmp table (
id int not null identity
, name varchar(100) -- you need name to be indexed
, urlSuffix int -- store the number (ot you'll have to use PATINDEX, etc. as previously shown)!
, url as name + ISNULL('_' + cast(NULLIF(urlSuffix, 0) as varchar(100)), '')
, unique (name, id) -- (trick) index on name
)
insert #tmp (name, urlSuffix)
select
src.name
, ISNULL(T.urlSuffix, -1) + ROW_NUMBER() OVER (PARTITION BY src.name ORDER BY (select 1))
from (values
('x')
, ('y')
, ('y')
, ('y')
, ('z')
, ('z')
) as src (name)
left join (
select
name
, MAX(T.urlSuffix) as urlSuffix
from #tmp AS T
GROUP BY name
) as T on (
T.name = src.name
)
insert #tmp (name, urlSuffix)
select
src.name
, ISNULL(T.urlSuffix, -1) + ROW_NUMBER() OVER (PARTITION BY src.name ORDER BY (select 1))
from (values
('a')
, ('b')
, ('b')
, ('b')
, ('z')
, ('z')
) as src (name)
left join (
select
name
, MAX(T.urlSuffix) as urlSuffix
from #tmp AS T
GROUP BY name
) as T on (
T.name = src.name
)
select
name, url
from #tmp
order by url
The solution to yur problem should lies in the use of ROW_NUMBER()
SQL Server 2008.
declare #pardate table ( pardateid int, pardatewhen datetime2(3) )
insert into #pardate values ( 1 , '2011-09-17 12:43' )
insert into #pardate values ( 2 , '2011-09-17 12:44' )
insert into #pardate values ( 3 , '2011-10-11 12:45' )
insert into #pardate values ( 4 , '2011-10-12 12:46' )
insert into #pardate values ( 5 , '2011-10-13 12:47' )
insert into #pardate values ( 6 , '2011-11-20 12:48' )
insert into #pardate values ( 7 , '2011-11-21 12:49' )
insert into #pardate values ( 8 , '2011-11-22 12:50' )
declare #child table ( childid int , pardateid int , childvalue char(6) )
insert into #child values ( 1 , 1 , 'aaaaaa' )
insert into #child values ( 2 , 2 , 'bbbbbb' )
insert into #child values ( 3 , 3 , 'cccccc' )
insert into #child values ( 4 , 4 , 'dddddd' )
insert into #child values ( 5 , 5 , 'cccccc' )
insert into #child values ( 6 , 6 , 'cccccc' )
insert into #child values ( 7 , 7 , 'eeeeee' )
insert into #child values ( 8 , 8 , 'ffffff' )
select pardatewhen , childvalue , COUNT(childvalue)
from #child childtable join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by pardatewhen , childvalue
I am trying to get a count of #child.childvalue every day, every hour, so there would be 8760 rows in my result.
First pass had a loop and a CONVERT which takes ~5 minutes to run with the actual result set (this is just a sample for illustation). I did create a CTE to make a calendar temp table (using http://www.sqlpointers.com/2006/07/generating-temporary-calendar-tables.html), and thought it could be joined somehow to add "empty values" into the result set.
I need to get a result set that looks like this
date hour count
...
2011-09-17 0 0
....
2011-09-17 12 2
....
2011-10-11 12 1
How can that be done efficiently?
Thanks.
try this.
;WITH cal AS
(SELECT CAST('2011-01-01' AS DATETIME) AS cal_date
UNION ALL
SELECT DATEADD(hour,1,cal_date)
FROM cal
WHERE cal_date < '2011-12-31 23:00'
)
, par AS
(
select CAST(pardatewhen AS DATE) AS pardate, DATEPART(hh,pardatewhen) AS parhour , COUNT(childvalue) as num
from #child childtable
join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by CAST(pardatewhen AS DATE), DATEPART(hh,pardatewhen)
)
SELECT CAST(cal.cal_date AS DATE) AS [date],DATEPART(hh,cal.cal_date) AS [hour],ISNULL(par.num,0) AS [childvalue_count]
FROM cal
LEFT JOIN par
ON CAST(cal.cal_date AS DATE) = par.pardate
AND DATEPART(hh,cal.cal_date) = par.parhour
OPTION (MAXRECURSION 9999)
Something like (have childvalue in your query but not in your example result?)
select Cast(pardatewhen as Date) as [date], DatePart(hour,pardatewhen) as [hour] , childvalue , COUNT(childvalue)
from #child childtable
join #pardate parenttable on childtable.pardateid=parenttable.pardateid
group by Cast(pardatewhen as Date), DatePart(hour,pardatewhen), childvalue
Note Date type was introduced in SQL 2008
Edit - restating my need since two guys with way more rank than me misunderstood my question, so I need to make this better...
I have a table like the below. I need to select all of the rows for the first group of 'sec1' rows where the 'ison' column is 1. So the query should first return the 'bbb' row, but if I set all rows to ison=0 and then make the 'ccc' rows ison=1, then I would get two rows of 'ccc' in the result set. Can anyone help me with my rank/top? Using MSSQL 2008.
create table #grp ( sec1 varchar(4) , sec2 varchar(4) , ison bit )
insert into #grp values ( 'aaa' , '001' , 0 )
insert into #grp values ( 'aaa' , '002' , 0 )
insert into #grp values ( 'bbb' , '001' , 1 )
insert into #grp values ( 'ccc' , '001' , 1 )
insert into #grp values ( 'ccc' , '001' , 1 )
Select * From
( Select
sec1 ,
sec2 ,
ison ,
RANK() Over ( partition by sec1 order by sec1,sec2 ) as rowrank
from #grp
where ison=1
) tmp
where rowrank=1
Thanks.
create table #grp ( sec1 varchar(4) , sec2 varchar(4) , ison bit )
insert into #grp values ( 'aaa' , '001' , 0 )
insert into #grp values ( 'aaa' , '002' , 1 )
insert into #grp values ( 'bbb' , '001' , 1 )
insert into #grp values ( 'ccc' , '001' , 1 )
insert into #grp values ( 'ccc' , '001' , 1 )
SELECT *
FROM #grp
WHERE sec1 = (
Select TOP(1) sec1
From
( Select
sec1 ,
sec2 ,
ison ,
RANK() Over ( partition by sec1 order by sec1,sec2 ) as rowrank
from #grp
where ison=1
) tmp
where rowrank=1
Order by sec1, Sec2
)