I need to make trigger to increase the 2nd digit when the new value found to be a duplicate.
For Instance, I have a unique filed with 10 digits value. I want when someone insert same number it increase the second left digit like 0100012345. How I can do that? Thank you.
FirstName LastName Code
Houssam Salim 0100012345 to be 0200012345
Try this
I have found a solution with instead off trigger
'/*
CREATE TABLE [Employee1]
(
[id] VARCHAR(20) PRIMARY KEY,
[name] VARCHAR(50)
)
CREATE TRIGGER AutoIncrement_Trigger
ON [Employee1]
instead OF INSERT
AS
BEGIN
DECLARE #ch CHAR
DECLARE #num INT
IF EXISTS (SELECT 1
FROM Employee1 e
JOIN inserted i
ON i.id = e.id)
BEGIN
SET #num=(SELECT max(CONVERT(INT, substring(e.id, 1, 2))) + 1
FROM employee1 e
JOIN inserted i
ON substring(e.id, 3, len(e.id)) = substring(i.id, 3, len(i.id)))
INSERT INTO [Employee1]
(id,
name)
SELECT '0' + CONVERT(VARCHAR(10), #num)
+ substring(i.id, 3, len(i.id)),
e.name
FROM Employee1 e
JOIN inserted i
ON i.id = e.id
END
ELSE
BEGIN
INSERT INTO [Employee1]
(id,
name)
SELECT inserted.id,
inserted.name
FROM inserted
END
END
*/
INSERT INTO [Employee1]
VALUES ('0100012345',
'John')
SELECT *
FROM [Employee1]
INSERT INTO [Employee1]
VALUES ('0100012345',
'John')
SELECT *
FROM [Employee1]
'
Related
I can't seem to troubleshoot my problem.
My stored procedure:
CREATE DEFINER=`myschoolusername`#`%` PROCEDURE `generate_volunteers`(in nfolks int)
BEGIN
set #i=0;
while #i < nfolks do
insert into Volunteer(firstname, lastname, dateofbirth)
values (((floor(1+(rand()*(4-1))), "Fred", "Wang", "Fatimah", "Marcella")),
((floor(1+(rand()*(3-1))), "Kaser", "Fang", "Kumar")),
DATE_ADD('1965-01-01', INTERVAL rand()*200000 DAY));
set #i = #i+1;
end while;
END
Additionally, here is my volunteer table in my MYSQL script:
drop table if exists Volunteer;
create Table Volunteer(
member_num int not null auto_increment primary key,
firstname varchar(20) not null,
lastname varchar(20) not null,
dateofbirth date not null
);
I am trying to insert 500 lines into this table, however error 1305 is coming up.
Any help is heavily appreciated, I am quite unsure of where to go from this point.
This logic doesn't do anything:
(floor(1+(rand()*(4-1))), "Fred", "Wang", "Fatimah", "Marcella"))
Although not the most efficient, this should be fine for 500 rows:
insert into Volunteer(firstname, lastname, dateofbirth)
select f.firstname, l.lastname,
DATE_ADD('1965-01-01', INTERVAL rand()*200000 DAY)
from (select 'Fred' as firstname union all
select 'Wang' union all
select 'Fatimah' union all
select 'Marcella'
) f cross join
(select 'Kaser' as lastname union all
select 'Fang' union all
select 'Kumar'
) l
order by rand()
limit 1;
I think you are actually trying to write:
insert into Volunteer(firstname, lastname, dateofbirth)
select elt(floor(rand() * 4) + 1,
'Fred', 'Wang', 'Fatimah', 'Marcella'
) as firstname,
elt(floor(rand() * 3) + 1,
'Kaser', 'Fang', 'Kumar'
) as lastname,
DATE_ADD('1965-01-01', INTERVAL rand()*200000 DAY);
I am currently using the SQL command
Select * from where name='john'
Is it possible to return 20 no matter the query, for example
Select * from where name='john' or return = 20
EDIT
If you have an oracle database you can do something like that:
SELECT *
FROM dual
WHERE 1=0
UNION
SELECT '20'
FROM dual;
check my answer
if exists (Select * from item where ItemName='ABC Daycare1')
begin
Select * from item where ItemName='ABC Daycare1'
end
else
select '20'
Try running this. This should return the top result (which is never 20 due to the custom sort) and then when the name doesn't match a value it returns 'Mark' and 20
SQL
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp (id int NOT NULL, name varchar(255) NOT NULL)
INSERT INTO #temp (id, name) VALUES (88,'John')
INSERT INTO #temp (id, name) VALUES (20,'Mark')
SELECT TOP 1
*
FROM #temp
WHERE (name = 'Mark' OR name = 'John')
ORDER BY (
CASE
WHEN id = 20 THEN 0 ELSE 1
END) DESC
MySQL - MySQL fiddle
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp (id int NOT NULL, name varchar(255) NOT NULL)
INSERT INTO #temp (id, name) VALUES (88,'John')
INSERT INTO #temp (id, name) VALUES (20,'Mark')
SELECT
*
FROM temp
WHERE (name = 'Mark' OR name = 'John')
ORDER BY (
CASE
WHEN id = 20 THEN 0 ELSE 1
END) DESC
LIMIT 1
Is there a way to define an identity column on another column? What I want to accomplish is a table that holds positions of an order and these orders can be put there anytime. So it could be that there are already lets say three positions in the table and it would look somewhat like this:
OrderNumber | OrderPosition
10001 1
10001 2
10001 3
And now I want to add another position without calculating the right value for the OrderPosition column. This is because I want to write new positions for multiple orders into the table and would like to avoid cursoring over the individual orders. I would prefer a solution wher OrderPosition is an identity column that is reseeded based on the OrderNumber column. So that If i add an order position for a new order it would start with 1 and if I add another position for order 10001 it would continue with 4.
Write a Scalar Function that returns the MAX(OrderPosition) based on OrderNumber. Then reference that function in the insert statement of orders
your requirement will not work for identity column.
You need to create custom logic to get from the normal columns and on combination based new no will generate.. like (read comments, only choose one logic)
declare #t table(OrderNumber int, OrderPosition int)
insert into #t values (10001, 1),(10001, 2),(10001, 3),(10001, 4)
select * from #t
--now insert new record with old orderno
declare #seq int = 1
declare #ordernumberNew int = 10001
--Eigher you can use :- insert to more understand
if( exists(select orderposition from #t where OrderNumber = #ordernumberNew ))
begin
set #seq = (select max(OrderPosition) + 1 from #t where OrderNumber = #ordernumberNew )
end
insert into #t values (#ordernumberNew , #seq )
select * from #t
--or another twist of above statement, insert directly as
insert into #t
values
(
#ordernumberNew,
case when exists (select orderposition from #t where OrderNumber = #ordernumberNew )
then (select max(OrderPosition) + 1 from #t where OrderNumber = #ordernumberNew )
else 1 end
)
select * from #t
--Now enter the not exist order no
set #ordernumberNew = 10006
insert into #t
values
(
#ordernumberNew,
case when exists (select orderposition from #t where OrderNumber = #ordernumberNew )
then (select max(OrderPosition) + 1 from #t where OrderNumber = #ordernumberNew )
else 1 end
)
select * from #t
I searched a while for this problem, but I can't solve it...
I want to check if a record doesn't exists in a mysql table and then insert a record.
Here is my code:
IF NOT EXISTS (SELECT * FROM personOffice WHERE personID = 2 AND officeID = 1)
BEGIN
INSERT INTO personOffice (personID, officeID) VALUES ('2', (SELECT officeID FROM offices WHERE title = 'Berlin'))
END
Mysql tells me that theres a syntax-error in my first line.
Thanks for your solutions.
INSERT INTO personOffice(personID, officeID)
SELECT '2', (SELECT officeID FROM offices WHERE title = 'Berlin') FROM dual
WHERE NOT EXISTS (SELECT * FROM personOffice WHERE personId = 2 AND officeID = 1)
You might try this :
INSERT INTO personOffice (personID, officeID)
VALUES ('2', (SELECT officeID FROM offices WHERE title = 'Berlin'))
WHERE NOT EXISTS (SELECT * FROM personOffice WHERE personID = 2 AND officeID = 1)
I'm looking to see if this is even possible. I am constructing a view that needs to have a sub query. However, the sub query needs to have the same where clause as the main SQL statement.
I need to have this in a view, because I have to do a JOIN from another table, and a stored procedure, nor a table UDF will work in my case.
I've constructed the following example to show what I'm trying to do:
IF OBJECT_ID('tempdb..#TableA') IS NOT NULL BEGIN DROP TABLE #TableA END
IF OBJECT_ID('tempdb..#TableB') IS NOT NULL BEGIN DROP TABLE #TableB END
Create Table #TableA
(
Id INT IDENTITY(1, 1)
, ValueA VARCHAR(10)
)
Create Table #TableB
(
Id INT IDENTITY(1, 1)
, TableAID INT
, ValueB VARCHAR(10)
)
INSERT INTO #TableA VALUES ('Company A'), ('Company B')
INSERT INTO #TableB VALUES (1, '05001'), (1, '05002')
INSERT INTO #TableB VALUES (2, '04001'), (2, '04003')
SELECT
DISTINCT
A.ValueA
, STUFF((SELECT
', ' + B.ValueB
FROM
#TableB B
INNER JOIN #TableA A on A.Id = B.TableAID
WHERE
B.ValueB IN ('05001', '05002') --This needs to be part of the main where clause
FOR XML PATH ('')), 1, 1, '') as TBValue
FROM
#TableA A
INNER JOIN #TableB B on B.TableAID = A.Id
WHERE
B.ValueB IN ('05001', '05002') --This will be passed in as the where clause for the View
IF OBJECT_ID('tempdb..#TableA') IS NOT NULL BEGIN DROP TABLE #TableA END
IF OBJECT_ID('tempdb..#TableB') IS NOT NULL BEGIN DROP TABLE #TableB END
The output from the above example give us this:
ValueA TBValue
Company A 05001, 05002
I need to be able to get the sub query to use the same values as the main where clause, OR reconstruct the query to use the primary where clause somehow for the sub data.
You can not pass parameters into a view. They are not designed to accept them. I believe you need to use a Table-Valued (Inline-Table) Function in this case. This will allow you to pass in the values you want and get a 'table' back.
You should be able to use the same code you've already written for your view, with minor changes in the function. You will have to declare your incoming variables, and outgoing table value.
CREATE FUNCTION tvf_SomeFunction
(
-- Add the parameters for the function here
#Value1 varchar(10),
#Value2 varchar(10)
)
RETURNS TABLE
AS
RETURN
DECLARE #TableA TABLE
(
Id INT IDENTITY(1, 1)
, ValueA VARCHAR(10)
)
DECLARE #TableB TABLE
(
Id INT IDENTITY(1, 1)
, TableAID INT
, ValueB VARCHAR(10)
)
INSERT INTO #TableA VALUES ('Company A'), ('Company B')
--INSERT INTO #TableB VALUES (1, '05001'), (1, '05002')
--INSERT INTO #TableB VALUES (2, '04001'), (2, '04003')
SELECT
DISTINCT
A.ValueA
, STUFF((SELECT
', ' + B.ValueB
FROM
#TableB B
INNER JOIN #TableA A on A.Id = B.TableAID
WHERE
B.ValueB IN (#Value1, #Value2) --This needs to be part of the main where clause
FOR XML PATH ('')), 1, 1, '') as TBValue
FROM
#TableA A
INNER JOIN #TableB B on B.TableAID = A.Id
WHERE
B.ValueB IN (#Value1, #Value2) --This will be passed in as the where clause
)
GO
Your final SQL would be as simple as the following:
SELECT *
FROM tvf_SomeFunction('05001', '05002')