Insert Multiple Rows to table after select and change Values rows - c#

:)
i have a table the Includ any row.
Names Rows my Table is:
ID_ImgPhoto(PK),IDPhoto(FK),Title,Small,Larg,AddedDate,AddedIp,Status.
i want read at the DataReader in asp.net and change string the String.replace() many Values Small And Larg row in my table.
i want Insert Multiple Rows to table after select and change The Same Values rows.
i want select many row With a Condition.
My select Code Is:
ALTER Procedure [dbo].[sp_Tbl_ImagePhotoGraphy_SelectRow]
#IDPhoto int
As
Begin
Select
[ID_ImgPhoto],
[IDPhoto],
[Title],
[Small],
[Larg],
[AddedDate],
[AddedIp],
[Status]
From Tbl_ImagePhotoGraphy
Where
[IDPhoto] = #IDPhoto
End
i Want changed value many rows after the give Value #IDPhoto And select Many Row my table and FOR EXAMPLE :
/*OLD DATA*/
ID_ImgPhoto IDPhoto Small Larg
1 1 OLD/1S.jpg OLD/1L.jpg
2 1 OLD/2S.jpg OLD/2L.jpg
3 1 OLD/3S.jpg OLD/3L.jpg
4 2 Oldsmall/1S.jpg OldLarg/1L.jpg
5 2 Oldsmall/1S.jpg OldLarg/1L.jpg
5 2 Oldsmall/1S.jpg OldLarg/1L.jpg
5 2 Oldsmall/1S.jpg OldLarg/1L.jpg
____________________________________________________________________
/*NEW DATA Afetr Select And Chnage Rows Value*/
ID_ImgPhoto IDPhoto Small Larg
1 1 NEW/1S.jpg NEW/1L.jpg
2 1 NEW/2S.jpg NEW/2L.jpg
3 1 NEW/3S.jpg NEW/3L.jpg
4 2 Oldsmall-2/1S.jpg OldLarg-2/1L.jpg
5 2 Oldsmall-2/1S.jpg OldLarg-2/1L.jpg
5 2 Oldsmall-2/1S.jpg OldLarg-2/1L.jpg
5 2 Oldsmall-2/1S.jpg OldLarg-2/1L.jpg

I FIND THIS Sulotion.:)
UPDATE yourtable set small=replace(small,'OLD/','NEW/'),larg=replace(larg,'OLD/','NEW/')

Related

What is the best way to update a table column based on a result of a sql select query C#?

The original query :
with Tr As (
SELECT
DocDtls.Warehouse,
Transactions.Code,
DocDtls.zDate,
Transactions.ID,
Transactions.QtyIn,
Transactions.QtyOut,
Transactions.BalanceAfter
FROM
DocDtls
INNER JOIN Transactions ON DocDtls.[PrimDocNum] = Transactions.[DocNum]
),
formatted_tr as (
select
ID,
Code,
QtyIn,
QtyOut,
BalanceAfter,
LAG(BalanceAfter, 1, 0) Over (
partition by Warehouse,
Code
order by
Code,zDate,ID
) Prev_BlncAfter
from
Tr
)
select ID,Code,QtyIn,QtyOut,BalanceAfter
,SUM(Prev_BlncAfter + QtyIn)-QtyOut As NewBlncAfter
from formatted_tr
group by ID,Code,QtyIn,QtyOut,BalanceAfter;
;
Explaining the idea :
Let's say that the query returns all transactions of Item X and there are 10 rows as result , I need to loop through all 10 rows and SET BalanceAfter( for the first transaction QtyIn-QtyOut , Any other transaction (PreviousBalanceAfter+QtyIn)-QtyOut) And so on .
What I've tried :
I tried to put the query result in a Datatable then filter it one more time using DataView to get the NewBlncAfter of the DataGridView current row ID only so the Dataview only have one row and save it in a variable - Working well so far - when I try to loop through all rows in my DataGridview and update BalanceAfter I got :
Must Declare Scalar Variable #Newblnc
You can find the whole code in here :
My Code
So Is there a direct way to update all transactions BalanceAfter to equal
EDIT #1 : I used #Charliface query and the result was :
I used the old query to compare the results , The BalanceAfter should equal NewBlncAfter in every row .
Edit #2 : Using SUM instead of LAG causing wrong calculation and if I used the query more than once the result in BalanceAfter is multiplied
ID Code QtyIn QtyOut BalanceAfter
9 100001 20000 0 20000
14 100001 0 6000 40000
21 100001 3500 0 60000
24 100001 0 3000 80000
The main idea and the desired result for example :
ID Code QtyIn QtyOut BalanceAfter
9 100001 20000 0 20000
14 100001 0 6000 14000
21 100001 3500 0 17500
24 100001 0 3000 14500
The formula is :
for the first transaction QtyIn-QtyOut , Any other transaction (PreviousBalanceAfter+QtyIn)-QtyOut And so on .
Firstly, I see no reason at all to pull all this data into C# only to then update row-by-row (which is highly inefficient). You can do this in a single batch update.
It's not quite clear what result you want, but it seems you want to just assign a running SUM calculation, rather than LAG.
Furthermore:
The second CTE is unnecessary and can be collapsed into the first.
Partitioning and ordering by the same column in an OVER makes no sense.
The final GROUP BY also makes no sense and appears unnecessary, as you are grouping by a primary key.
WITH Tr AS (
SELECT
d.Warehouse,
t.Code,
d.zDate,
t.ID,
t.QtyIn,
t.QtyOut,
t.BalanceAfter,
SUM(t.QtyIn - t.QtyOut) OVER (
PARTITION BY d.Warehouse, t.Code
ORDER BY d.zDate, t.ID
ROWS UNBOUNDED PRECEDING
) BlncAfter
FROM
DocDtls d
INNER JOIN Transactions t ON d.PrimDocNum = t.DocNum
WHERE t.Code = #VariableCode
)
UPDATE Tr
SET BalanceAfter = BlncAfter;
One final point: why bother storing this information in a new column at all? Why not just calculate it when you need to, using SUM OVER?

C# SQL How to select and process N records, and then update them?

I retrieve some records by running this query:
SELECT * FROM [MyTable] order by [Date] OFFSET N ROWS FETCH NEXT 500 ROWS ONLY
Where N is a number that starts from 0 and increment by 500. There are multiple instances of this application and each instance gets 500 records.
Now the question is, how do I update the retrieved records? The records don't have primary key. I tried something like this but the syntax isn't right:
UPDATE [MyTable] SET [status] = 1 order by [Date] OFFSET N ROWS FETCH NEXT 500 ROWS
Note: I can't use WHERE status = 0 because I want one instance to only deal with the records it retrieved.
Any idea?
You could try something like this:
UPDATE x
SET [status] = 1
FROM (
SELECT * FROM [MyTable] order by [Date] OFFSET N ROWS FETCH NEXT 500 ROWS ONLY
) x
UPDATE x
SET x.[status] = 1
OUTPUT deleted.*
FROM
(
SELECT *
FROM [MyTable]
WHERE
[status] = 0
ORDER BY [Date]
OFFSET N ROWS FETCH NEXT 500 ROWS ONLY
) x;
Be careful though, your "fetch next" is preventing any rows from being returned if there are not at least 500 to return. Maybe that's what you want.

order by desc didnt not arrange my data

once there are new data insert to my DB my rcount will increase by 1, what am i trying to do is to display latest inserted record to users, but i cant get my expected result
rCount in my datebase
1
2
3
4
5
output
5
4
3
1
2
expected output
5
4
3
2
1
"SELECT *, ROW_NUMBER() OVER (ORDER BY rCount DESC) AS 'RowNumber'
FROM [MovieListTable]"
Instead of using the row number you should introduce a numeric primary key (i.e. Id) and make it an Identity column. Now you can just select the row that has the maximum Id value:
select top 1 * from MovieListTable order by Id desc
Or to get all rows:
select * from MovieListTable order by Id desc

Update Sql Table By Listview Items

My listview has three columns and rows as below:
Trans ID Name Amount
1 mahesh 1000
2 mahendra 2000
3 kirti 3000
Is there possible to update/insert/delete on sql table like below:
Delete from mytable where transID = 3
Update mytable set name = ‘mahendra’ where transID=2
Insert into mytable(transID,name,amount)values(#transID,#name,#amount)
Is it possible The above three sql transact statement with Listview?.
I am Looking For Code Example Here.
If you mean is update the DB Table by your listview ...
I think it is like ↓
1) delete from yourtable where transid in (all ids in your listview);
2) foreach(var item in listview.Items) Insert into yourtable(transID,name,amount)values(#transID,#name,#amount)

I want to search in SQL Server with must have parameter in one colunm

I am using c# and SQL Server 2008.
I have table like this
id | propertyTypeId | FinishingQualityId | title | Description | features
1 1 2 prop1 propDEsc1 1,3,5,7
2 2 3 prop2 propDEsc2 1,3
3 6 5 prop3 propDEsc3 1
4 5 4 prop4 propDEsc4 3,5
5 4 6 prop5 propDEsc5 5,7
6 4 6 prop6 propDEsc6
and here is my stored code (search in the same table)
create stored procdures propertySearch
as
#Id int = null,
#pageSize float ,
#pageIndex int,
#totalpageCount int output,
#title nvarchar(150) =null ,
#propertyTypeid int = null ,
#finishingqualityid int = null ,
#features nvarchar(max) = null , -- this parameter send like 1,3 ( for example)
begin
select
row_number () as TempId over( order by id) ,
id, title, description,
propertyTypeId, propertyType.name,
finishingQualityId, finishingQuality.Name,
freatures
into #TempTable from property
join propertyType on propertyType.id= property.propertyTypeId
join finishingQuality on finishingQuality.id = property.finishingQualityId
where
property.id = isnull(#id,property.id ) and proprty.PropertyTypeId= isnull(#propertyTypeid,property.propertyTypeId)
select totalpageconunt = ((select count(*) from #TempTable )/#pageSize )
select * from #TempTable where tempid between (#pageindex-1)*#pagesize +1 and (#pageindex*#pageSize)
end
go
I can't here filter the table by feature I sent. This table has too many rows I want to add to this stored code to filter data for example when I send 1,3 in features parameter I want to return row number one and two in the example table I write in this post (want to get the data from table must have the feature I send)
Many thanks for every one helped me and will help
Sending a delimited list of values to match with a delimited value in a column will generally get you into trouble as the only way to do it is to split each value out from the string. (Also why indexing them is pointless)
You should create an additional table containing the id from your property table & a row for each feature;
property_id feature
----------- -------
5 5
5 7
Then matching is much simpler. You can send the procedure the list of features you want to match ideally using a table valued parameter or alternatively a table function.

Categories

Resources