ต้องการ insert ข้อมูลลง 2 table ลงพร้อมๆ กันทำไงอะครับพี่ๆ asp classic sql server นะครับ
เขียน Insert 2 Query ก็ได้แล้วครับ ถ้าต้องการความถูกต้องให้ใช้ Transaction ครอบครับ
Code (ASP)
<% Option Explicit %>
<html>
<head>
<title>ThaiCreate.Com ASP & SQL Server Tutorial</title>
</head>
<body>
<%
Dim Conn,strSQL,objExec
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "Driver={SQL Server};Server=localhost;Database=mydatabase;UID=sa;PWD=;"
On Error Resume Next '*** Error Resume ***'
'*** Transaction Start ***'
Conn.BeginTrans
'*** Save Form ***'
strSQL = ""
strSQL = strSQL &"INSERT INTO customer "
strSQL = strSQL &"(CustomerID,Name,Email,CountryCode,Budget,Used) "
strSQL = strSQL &"VALUES "
strSQL = strSQL &"('"&Request.Form("txtCustomerID")&"','"&Request.Form("txtName")&"', '"&Request.Form("txtEmail")&"' "
strSQL = strSQL &",'"&Request.Form("txtCountryCode")&"','"&Request.Form("txtBudget")&"', '"&Request.Form("txtUsed")&"') "
Set objExec = Conn.Execute(strSQL)
Set objExec = Nothing
'*** Already Exist Save Form (CustomerID is PK)***'
strSQL = ""
strSQL = strSQL &"INSERT INTO customer "
strSQL = strSQL &"(CustomerID,Name,Email,CountryCode,Budget,Used) "
strSQL = strSQL &"VALUES "
strSQL = strSQL &"('"&Request.Form("txtCustomerID")&"','"&Request.Form("txtName")&"', '"&Request.Form("txtEmail")&"' "
strSQL = strSQL &",'"&Request.Form("txtCountryCode")&"','"&Request.Form("txtBudget")&"', '"&Request.Form("txtUsed")&"') "
Set objExec = Conn.Execute(strSQL)
Set objExec = Nothing
If Err.Number = 0 Then
'*** Commit Transaction ***'
Conn.CommitTrans
Response.write("Save Done.")
Else
'*** Rollback Transaction ***'
Conn.RollbackTrans
Response.write("Error Save ["&strSQL&"] ("&Err.Description&")")
End If
Conn.Close()
Set objExec = Nothing
Set Conn = Nothing
%>
</body>
</html>
Go to : ASP SQL Server Transaction (BeginTrans,CommitTrans,RollbackTrans)
Date :
2011-04-20 06:25:51
By :
webmaster
Code (ASP)
strSQL = ""
strSQL = strSQL & "INSERT INTO Village "
strSQL = strSQL & "(Village_name,Village_num,Village_tum,Village_mun,Village_amphur,Village_province)"
strSQL = strSQL &"VALUES "
strSQL = strSQL &"('"&Request.Form("txtVillage_name")&"','"&Request.Form("txtVillage_num")&"','"&Request.Form("txtVillage_tum")&"' "
strSQL = strSQL &",'"&Request.Form("txtVillage_mun")&"','"&Request.Form("txtVillage_amphur")&"','"&Request.Form("txtVillage_province")&"') "
Set objExec = Conn.Execute(strSQL)
Set objExec = Nothing
'*** Already Exist Save Form (CustomerID is PK)***'
strSQL = ""
strSQL = strSQL &"INSERT INTO Project "
strSQL = strSQL &"(Pro_budget) "
strSQL = strSQL &"VALUES "
strSQL = strSQL &"('"&Request.Form("txtPro_budget")&"') "
Set objExec = Conn.Execute(strSQL)
Set objExec = Nothing
insert แบบนี้ error ครับ
Date :
2011-04-20 09:52:53
By :
Prayayut
Error อะไรครับ ?
Date :
2011-04-20 09:58:13
By :
webmaster
Error Save [INSERT INTO Project (Pro_budget) VALUES ('yut, ') ] ([Microsoft][ODBC SQL Server Driver][SQL Server]Conversion failed when converting the varchar value 'yut, ' to data type int.)
ตัวนี้ครับ งง?
Date :
2011-04-20 10:02:39
By :
Prayayut
varchar value 'yut, '
varchar value 'yut',
Date :
2011-04-20 10:27:29
By :
blacklion
ยังรอ คำตอบนะครับ อิอิ
Date :
2011-04-20 11:09:28
By :
Prayayut
Load balance : Server 05