sql语句将行插入表中,其中不包含特定单词 [英] sql Statement to Insert rows into a table, where a specific word could not included

查看:98
本文介绍了sql语句将行插入表中,其中不包含特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在表格中插入行时,特定列不能允许特定单词或金额。



示例,我有一个名为 Student_Master的表(姓名,年龄,部门),其中年龄必须大于 18 才能包含在表格中,否则无法插入..



如何在插入查询中写这个...



先谢谢了......

When i insert rows into a table, particular column could not allow particular word or Amount.

Example,i have a table called Student_Master(Name, Age,Department), where a Age must have greater than 18 to be included in the table Otherwise it could not insert..

How can i write this in insert queries..

Thanks in Advance......

推荐答案

在INSERT查询中执行此操作并不是一个好主意 - 在代码中的多个位置重复它并且出错会很容易,或者错过一个截止年龄变为21说。



相反,这可能最好通过使用数据库上的存储过程来检查或插入:

It's not a good idea to do this as part of your INSERT query - it's too easy to have it repeated in multiple places in your code and make a mistake, or miss one if the cut-off age changes to 21 say.

Instead, this would probably be best handled by using a stored procedure on the database to check or insert:
CREATE PROCEDURE InsertWithAgeCheck 
	@PName VARCHAR(100),
	@Age int,
	@Dept VARCHAR(100) 
AS
BEGIN
IF (@Age < 18) 
   RAISERROR('Too Young',18, 1)
ELSE
   INSERT INTO MyTable ([Name], [Dept], [Age]) VALUES (@PName, @Dept, @Age)
END
GO


这篇关于sql语句将行插入表中,其中不包含特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆