| |

VerySource

 Forgot password?
 Register
Search
View: 1387|Reply: 13

Novice seeking a SQL stored procedure (online, etc ...)

[Copy link]

3

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-2-12 07:00:01
| Show all posts |Read mode
Table temp:
ID Product ID time v1 v2 v3 v4 sums

v1--v4 default value 1
sums = v1 + v2 + v3 + v4

Question: When only “Product ID” is entered, the table is automatically completed to get the current system time and sum values

Example: execute the statement insert temp (product ID) values ​​('2001'), and query the table to get:

ID Product ID time v1 v2 v3 v4 sums
1 2001 17:20 1 1 1 1 4
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-4-8 15:15:01
| Show all posts
Just set the default value.
Time: getdate ()
v1: 1
v2: 1
v3: 1
v4: 1
sums: v1 + v2 + v3 + v4
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-4-8 19:45:01
| Show all posts
Use triggers better

create trigger auto_update on temp
for insert
as
   update temp set time = getdate (), sums = v1 + v2 + v3 + v4 from inserted a where a. product ID = product ID
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-4-8 20:15:02
| Show all posts
I want to use triggers for auto completion
Reply

Use magic Report

3

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-4-8 22:30:01
| Show all posts
Thank youcv4bf5d65fd
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-4-8 22:45:01
| Show all posts
Use triggers better

create trigger auto_update on temp
for insert
as
   update temp set time = getdate (), sums = v1 + v2 + v3 + v4 from inserted a where a. product ID = product ID

-----------------------------------------------
Where sums = v1 + v2 + v3 + v4, the values ​​of these 4 fields are not entered when input, so this trigger cannot be used
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-4-9 09:45:02
| Show all posts
Just assign 1 to other columns
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-4-9 10:45:01
| Show all posts
create table T (id int identity, product ID varchar (10),
Time char (5) default convert (char (5), getdate (), 108),
v1 int default 1,
v2 int default 1,
v3 int default 1,
v4 int default 1,
sums as v1 + v2 + v3 + v4
 )

insert T (Product ID) values ​​('2001')

select * from T
--result
id Product ID time v1 v2 v3 v4 sums
----------- ---------- ----- ----------- ------------ --------- ----------- -----------
1 2001 17:22 1 1 1 1 4

(1 row (s) affected)
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 United States

Post time: 2020-4-9 11:45:02
| Show all posts
Use the default values ​​and calculated columns
Reply

Use magic Report

3

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-4-9 23:45:01
| Show all posts
Problem: Execution result
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list