简体   繁体   English

如何在表中插入图像路径

[英]how to insert imagepath in table

I have a table named employee with the following columns: 我有一个名为employee的表,其中包含以下各列:

empid int (autoincrement,PK), 
empname varchar,
imagepath varchar

I want to insert empid , empname , and imagepath where image name should be the value of auto-increment id like ~/images/1.jpg (here 1 is auto-incremented id of that row) 我想插入empidempnameimagepath ,其中image name应该是~/images/1.jpg类的自动递增id的值(此处1是该行的自动递增id)

this is perfect code or not ??? 这是不是完美的代码???

insert into employee (name, path) 
values
(
 'sarju',
 (select '~/images/'+Cast(ISNULL(max(empid),0)+1 as varchar(50))+'.jpg' from emp)
)

It's not a perfect code because MAX(ID)+1 can be different from next Auto increment value. 这不是完美的代码,因为MAX(ID)+1可能与下一个自动递增值不同。 You can use IDENT_CURRENT function 您可以使用IDENT_CURRENT函数

INSERT INTO employee (name,path) 
SELECT 'sarju','~/images/'+CAST(IDENT_CURRENT('employee') AS VARCHAR(50))+'.jpg'

insert into employee (name,path) values('sarju',(select '~/images/'+Cast(ISNULL(max(empid),0)+1 as varchar(50))+'.jpg' from emp)) 插入员工(名称,路径)值('sarju',(从emp中选择'〜/ images /'+ Cast(ISNULL(max(empid),0)+1作为varchar(50))+'。jpg') )

yes, this is possible 是的,这是可能的

I may get dinged for this because a lot of people don't like triggers, but this is a perfect example of what they were built for (and I honestly want the world to know that): 我可能对此感到不满意,因为很多人不喜欢触发器,但这是它们的用途的完美示例(我真的希望世界知道这一点):

CREATE TRIGGER update_imagepath ON dbo.employee FOR insert AS

UPDATE employee
SET imagepath = '~/images/' + CAST((SELECT empid FROM inserted) AS VARCHAR) + '.jpg'
WHERE empid = (SELECT empid FROM inserted)

they do serve a purpose, and things like this are exactly that. 他们确实有一个目的,像这样的事情就是这样。 See, you just need to INSERT the name of the employee and the TRIGGER will run automatically. 瞧,您只需要INSERT雇员的姓名, TRIGGER就会自动运行。 This is honestly the most straight forward way of handling this. 老实说,这是处理此问题的最直接的方法。 The other benefit of this - if you bulk inserted a bunch of employees or ran insert statements from Management Studio, the trigger still runs! 这样做的另一个好处-如果您批量插入了一堆员工或从Management Studio运行插入语句,则触发器仍将运行! You don't need to dig up that crazy INSERT statement! 您无需挖掘疯狂的INSERT语句!

Edit: While this work, I have to say that Nenad Zivkovics solution is better. 编辑:在这项工作中,我不得不说Nenad Zivkovics解决方案更好。

You could use SCOPE_IDENTITY() to get the latest created ID. 您可以使用SCOPE_IDENTITY()获取最新创建的ID。 That would also be safer so that you dont get the wrong ID. 这样也更安全,这样您就不会得到错误的ID。

insert into employee (name) 
values ( 'sarju')

declare @employeeId as int = SCOPE_IDENTITY()

update employee set 
path = '~/images/' + cast(@employeeId as varchar(50)) + '.jpg' 
where empid = @employeeId

If you have only one specific folder where will be stored your images, you can just make your field imagepath as computed 如果只有一个特定的文件夹用于存储图像,则只需将字段imagepath计算为

CREATE TABLE employee 
(
    empid int IDENTITY(1,1) NOT NULL,
    empname VARCHAR(50) NOT NULL,
    imagepath AS ('~/images/'+cast(empid AS VARCHAR(50))+'.jpg')
)

INSERT INTO employee (empname) 
VALUES ('sarju')
INSERT INTO employee (empname) 
VALUES ('sarju2')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM