需要一些SQL建议
我正在使用一个名为WinCC的程序,其中我使用的是Vb脚本语言和SQL。
我有一个SQL表,其中有3列,TagName,Value和Date
我需要为所选月份中的每一天显示每个TagName的值。
以下SQL查询为我每月的每一天提供相同的值。当我在这个月的每一天都有不同的价值观时。
Tags(0) = "Tag1"
Tags(1) = "Tag2"
TempTags(0) = "Temp1"
TempTags(1) = "Temp2"
ConnectionString = "Provider=SQL Native Client;Server=WIN81 \WINCC;Database=Teams;Trusted_Connection=yes"
Set Connection = CreateObject("ADODB.Connection")
Set rs = CreateObject ("ADODB.Recordset")
Connection.ConnectionString = ConnectionString
Connection.Open
For i = 0 To 2
SQLStr = "select Value FROM atrperformancestats WHERE TagName = '"+Tags(i)+"'And Month(Date)='"+date+"'"
rs = Connection.Execute(SQLStr)
HMIRuntime.Tags(TempTags(i)).Write rs("Value")
Next
我已经尝试更改我的SQL查询以获取用户选定月份中每天的值,但我失败了......请帮忙
非常感谢
答案 0 :(得分:0)
如果查询返回多行,则需要While
循环来处理结果集。下面是一个参数化示例,假设date
是表示月份的整数。
Const adVarChar = 200
Const adInteger = 3
Const adParamInput = 1
Const adCmdText = 1
Tags(0) = "Tag1"
Tags(1) = "Tag2"
TempTags(0) = "Temp1"
TempTags(1) = "Temp2"
ConnectionString = "Provider=SQL Native Client;Server=WIN81 \WINCC;Database=Teams;Trusted_Connection=yes"
Set Connection = CreateObject("ADODB.Connection")
Set Command = CreateObject("ADODB.Command")
Connection.ConnectionString = ConnectionString
Connection.Open
Command.ActiveConnection = Connection
Command.CommandType = adCmdText
Command.CommandText = "SELECT Value FROM atrperformancestats WHERE TagName = ? And Month(Date) = ?;"
Set parmTagName = command.CreateParameter("TagName", adVarchar, adParamInput, 100)
Command.Parameters.Append(parmTagName)
Set parmDate = command.CreateParameter("Date", adInteger, adParamInput)
command.Parameters.Append(parmDate)
For i = 0 To 1
parmTagName.Value = Tags(i)
parmDate.Value = date
Set rs = Command.Execute()
While rs.EOF = False
HMIRuntime.Tags(TempTags(i)).Write rs("Value").Value
rs.MoveNext
WEnd
rs.Close
Next
答案 1 :(得分:0)
在WinCC中,SCADA程序不是很苛刻,我最终找到了一种方法......
j = 0
SQLStr = "Select Value FROM table1 WHERE Name = '" + Names(i) + "' And Month(Date) = '" + MonthSelect + "' order by date asc"
rs.Open SQLStr, Connection
Do Until j = 31
HMIRuntime.Tags(arrayInt(i)).Write rs("Value")
rs.MoveNext
i = i +1
j = j +1
Loop
它有效!!!
感谢所有的帮助,你对循环的推荐让我思考