lat为10.2和 lon为10.9作为输入 现在任务是lat和lon将插入latlongtransactions带有提到的devId的表但首先它将检查来自devicemaster表的devId和密码。(这里是devicemaster表devId是主键和另一个表的外键。) 等待您的回复。解决方案 您好, 查看此... 创建 程序 Insertvalues ( @ pa_devId int , - 或根据您的数据类型 @ pa_pass varchar ( 100 ), - 或根据您的数据类型 @ pa_lat decimal ( 10 , 2 ), - 或根据您的数据类型 @ pa_lon decimal ( 10 , 2 ), - 或根据您的数据类型 @ OutPutMess varchar ( 20 ) - 或根据您的要求 ) 作为 开始 设置 @ OutPutMess = ' ' 声明 @ cnt int 设置 @ cnt = 0 选择 @ cnt =计数(*)来自 devicemaster 其中 devId = @ pa_devId 和密码= @ pa_pass 如果 @ cnt > 0 开始 插入 进入 latlongtransactions(devId,lat,lon) 值( @ pa_devId , @ pa_lat , @ pa_lon ) 设置 @ OutPutMess = ' 完成' - 或根据您的要求 end else 开始 设置 @ OutPutMess = ' 错误' - 或根据您的要求 结束 选择 @OutPutMess 结束 希望这会对你有所帮助。 干杯 INSERT INTO latlongtransactions(devId,lat,lon) SELECT 12345 , 10 。 2 , 10 。 9 FROM dual WHERE EXISTS ( SELECT * FROM devicemaster WHERE devId = 12345 AND password = 9999 ) here i have 2 tables latlongtransactions(devId, lat, lon) and devicemaster(devId, password) Now my task is i have to insert lat and lon value in the latlongtransactions table by matching the devId with devicemaster table where password and devId is being checked inputted by the user. Say for example, i am giving 12345 as devId, 9999 as password , lat as 10.2 and lon as 10.9 as inputs Now the task is lat and lon will be inserted into latlongtransactions table with the mentioned devId but firstly it will check the devId and password from devicemaster table.(here in devicemaster table devId is the primary key and a foreign key to the another table.) Waitng for your reply. 解决方案 Hi,Check this...Create Procedure Insertvalues(@pa_devId int,--or as per your datatype@pa_pass varchar(100),--or as per your datatype@pa_lat decimal(10,2),--or as per your datatype@pa_lon decimal(10,2),--or as per your datatype@OutPutMess varchar(20)--or as per your requirement)AsBeginSet @OutPutMess=''Declare @cnt intSet @cnt=0Select @cnt = Count(*) from devicemaster where devId=@pa_devId and password=@pa_passif @cnt > 0 Begininsert into latlongtransactions (devId, lat, lon) values (@pa_devId,@pa_lat,@pa_lon)Set @OutPutMess='Done'--or as per your requirementendelseBeginSet @OutPutMess='Error'--or as per your requirementEndSelect @OutPutMessEndHope this will help you.CheersINSERT INTO latlongtransactions(devId, lat, lon) SELECT 12345 , 10.2, 10.9 FROM dual WHERE EXISTS (SELECT * FROM devicemaster WHERE devId = 12345 AND password = 9999 ) 这篇关于通过在sql中连接2个表来插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 10:55