Display in TextBox working with three tables - sql-server

I have three table as below:
I have a dropdownlist and I want to display "Libelle" (Table 'Grade') which is connected with Table 'EvolutionGrade' ((main table)) According to "PPR" in 'Agent' Table.
I'm working with SQL Server: this is my query, I don't know if it's correct, I have to take Libelle in table Grade in display it in TextBox where Grade.codegrade = EvolutionGrade.codegrade (something like that) according to PPR in table Agent.
if exists ( select CodeGrade from Grade where CodeGrade = (select CodeGrade from Agent where PPR =#ppr))
begin
update Grade set Libelle=#lblgrade where CodeGrade = (select CodeGrade from Agent where PPR =#ppr)
end
else
insert into Grade (Libelle) values ( #lblgrade)

why do you have 2 the same table column? primary key must be different to other columns. Why not Eval_num?

Related

SQL Server : I'm trying to create a trigger that when inserted data in table Reservations it updates only specific row but not all of them

I'm kinda new to SQL Server and I have this task from school I'm trying to solve. I have 3 tables Flight, number_seats and reservations.
I'm trying to create a trigger so when data is inserted into table Reservations, it subtract 1 seat from the table Number_Seats in column Total_Number_Seats.
The problem I'm having is that my WHERE targets all of the rows so when I insert data into table Reservations, it affects all rows and not just the particular row with same FLIGHT_ID.
I realise my WHERE clause is not specific enough but I can't figure out how to write it so it targets specific row that got inserted. So please help if you can :).
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[ajSad]
ON [dbo].[Reservations]
AFTER INSERT
AS
BEGIN
UPDATE Number_Seats
SET Total_Number_Seats = Total_Number_Seats - 1
FROM Number_Seats
JOIN Flights ON Number_Seats.FLIGHT_ID = FLIGHTS.FLIGHT_ID
JOIN Reservations ON Flights.Flight_ID = Reservations.Flight_ID
WHERE Reservations.FLIGHT_ID = Number_Seats.Flight_ID
END
INSERT INTO reservations (ID_Rezervacije, ID_Klijenta, FLIGHT_ID)
VALUES (5, 5, 5);
Your trigger needs to use the inserted virtual table to only update the row(s) for the relevant flight_Id(s)
The inserted table in the context of the trigger will contain all the rows that were inserted in a single batch - this could be one or many and you need to assume it can be more than one.
You need to count the number of new reservations (presumably it's 1 row per reservation) and group by each flight_Id and then decrement the total number for each flight_Id
with t as (
select flight_Id, Count(*) tot
from inserted
group by flight_Id
)
update ns set
total_number_seats -= tot
from t
join number_seats ns on ns.flight_Id=t.flight_Id

SQL Server - Replacing NULL values with mode of column grouped by another column

I have a table (call it oldtable) and the relevant columns are name, group, zip code. I have selected those into a new table (call that newtable). My issue is that some of the zip codes in the first table are NULL. I want to replace the NULL zip codes with the mode (most common value) of their group.
For example, say a row in newtable looks like this:
Name Group ZipCode
Blah G1 NULL
I want to replace that NULL with the most common zip code over all the people in G1 in oldtable. I am having trouble even getting started on pulling the mode of one column when grouped by another column.
I am using Microsoft SQL Server 2016.
This can be done using CROSS APPLY on an UPDATE.
UPDATE n SET
zipcode = x.zipcode
FROM newtable n
CROSS APPLY( SELECT TOP 1 zipcode, COUNT(*) cnt
FROM newtable o
WHERE n.[group] = o.[group]
GROUP BY zipcode
ORDER BY cnt DESC) x
WHERE n.zipcode IS NULL;

SQL scheduled jobs Failed Notification

I need idea or a way to check if my schedule jobs work and insert the right data. I Have a function that call a stored procedure to count inserted data from today and insert into specific field.
like this
SET #male = (select count(idUser) from (select idUser from #tmpLog2 where sex = 1 AND CAST(catchTime as date) = #DATE group by idUser)u);
SET #female = (select count(idUser) from (select idUser from #tmpLog2 where sex = 0 AND CAST(catchTime as date) = #DATE group by idUser)u);
INSERT INTO CatchLog
(
[male],
[female]
)
VALUES
(
#male,
#female
)
the stored procedure works OK, but sometime when today have a lot of data, the result inserted 0 for male / female.
It's possible to have 0 data inserted when today data really no male / female.
but sometime it inserted 0 but there are male n female data. Anyone can help me to check, how to check if the data inserted true and give some report or insert into error Log if data inserted not true?
sorry for my bad english
I dont know what column are you using into Count() Function. But if you are using Nullable column into it then it may gives 0 even if record were added as column value is null.
Consider to use Primary key column or not Nullable column in count() argument.

SQL server : Inserting/updating missing data from one table to another

I have two table "Container" and "Control". These are existing tables and there is no foreign key relationship between the two. These are also very old tables so are not normalized. And I cannot change the structure now.
Below is the structure of the two tables.
Container table :
Control Table :
The Name field in Control table contains CTableName+CPName from Container table.
I want to update the columnName field of Control table with the value of CID column of Container table. and also want to insert one more record (for ctable2 i.e the fourth row in final Control table below) in Control table.
The tablename and columnname columns have will always be have default values.
The final Control table should look like this:
How do I do this?
I hope you want to apply this fix because you want normalize your table structure.
Try this:
First step:
In this way you'll UPDATE all Control rows with the value of Container table where the couple fields CTableName and CPName are the same of Name (excluding the rows of Container with the same couple fields)
UPDATE Control
SET ColumnValue = (
SELECT c.CID
FROM Container c
WHERE c.CTableName + '+' + c.CPName = Control.Name
AND NOT EXISTS(
SELECT 'PREVIOUS'
FROM Container c2
WHERE c.CTableName = c2.CTableName
AND c.CPName = c2.CPName
AND c.CID < c2.CID
)
),
TableName = 'default', ColumnName = 'default'
WHERE ColumnValue IS NULL
Second step:
Adding elements don't present in Control table
INSERT INTO Control (field list)
SELECT field list
FROM Container co
WHERE NOT EXISTS(
SELECT 'in_control'
FROM Control ct
WHERE co.CID = ct.ColumnValue
)
After these two steps you can drop column Name in Control table
I am an Oracle plsql programmer and worked with Sql-server as well.
First you should describe the relationship between the 2 tables, in the end i could figger it out but it's better you explain it yourself.
To update a table with information from another table you should ask yourself:
- when should the update take place?
- what are the conditions to start the update?
- how should the update be done?
In Oracle there is a database object called a trigger. It's quite a handy object and probably just what you need. I believe that sql-server has it too.
Pls fee free to ask any questions but do read the sql-server appropriate manual as well.
Good luck, Edward.

For Loop in Stored Procedure (SQL Server 2008)

I need to make a stored procedure to insert employees monthly salary in there payroll table. can anybody give example?
There are two tables
Employees (EmployeeID, EmployeeName, EmployeeStatus, BasicSalary)
and
EmployeePayroll (PayrollID, EmployeeID, VoucherNo, BasicSalary, SalaryMonth)
Get Total Employees From Employees Table WHERE EmployeeStatus IN ('Active')............for example let's say (50 employees)
Make a Loop for all these 50 employees and Insert salary payment voucher in table (EmployeePayroll).
In EmployeePayroll table it will be inserted with auto generated voucher no for example:
SET #PayrollID = (SELECT MAX(PayrollID) AS PayrollID FROM HR.EmployeePayroll)
SET #VoucherNo = ('SPV-K-' + CAST(DATEPART(YY,GETDATE())AS VARCHAR) + CAST(DATEPART(MM,GETDATE())AS VARCHAR) + CAST(DATEPART(DD,GETDATE())AS VARCHAR) + '-00' + #PayrollID)
So records will be like following:
PayrollID.......EmployeeID.......VoucherNo......BasicSalary.........SalaryMonth
1..................1...........SPV-K-11501-001.....250..................1
2..................2...........SPV-K-11501-002.....300..................1
3..................3...........SPV-K-11501-004.....400..................1
You don't need a loop for insert operations that get their data from other tables. As gbn suggested make the voucherID an IDENTITY column so it autoincrements on each insert. Then use an insert statement like this:
INSERT INTO EmployeePayroll (EmployeeID, VoucherNo, BasicSalary, SalaryMonth)
SELECT
EmployeeID,
( 'SPV-K-' + 'rest of your voucher calculation'),
BasicSalary, 1
FROM Employees WHERE EmployeeStatus IN ('Active')
Note that integrating the voucher-ID into the voucher-number is not possible this way and also desirable, IMHO. A better way would be to save the current date as a column "payout-day" and then generate the voucher number when selecting from this table. (Or better create a view for reading from this table and put the "VoucherNo" calculation into that views SELECT statement)

Resources