在我编写的 iPhone 应用程序中,我连接到用于执行 SQL Server 存储过程的 Web 服务。我发送了一些简单的查询,它工作正常。
我尝试了一些简单的存储过程,是的,它再次运行良好。问题在于这个特定的存储过程转换为 NSString。我意识到缺少为某些特定字符添加转义字符。我为方括号“[]”和“%”添加了转义字符。我说转义字符是问题所在,因为执行后出现的错误是“存储过程中的语法错误”。
但是当我直接在数据库中执行此操作(即通过 SQL 命令行客户端在本地执行)时,没有收到任何错误......它工作正常。因此,将其转换为 NSString 似乎是一个问题。请帮忙。
DECLARE @ts_now bigint = (SELECT cpu_ticks/(cpu_ticks/ms_ticks) FROM sys.dm_os_sys_info);
SELECT TOP(10) SQLProcessUtilization AS \\[SQL Server Process CPU Utilization\\],
SystemIdle AS \\[System Idle Process\\],
100 - SystemIdle - SQLProcessUtilization AS \\[Other Process CPU Utilization\\],
DATEADD(ms, -1 * (@ts_now - \\[timestamp\\]), GETDATE()) AS \\[Event Time\\]
FROM (
SELECT record.value('(./Record/@id)\\[1\\]', 'int') AS record_id,
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)\\[1\\]', 'int')
AS \\[SystemIdle\\],
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)\\[1\\]',
'int')
AS \\[SQLProcessUtilization\\], \\[timestamp\\]
FROM (
SELECT \\[timestamp\\], convert(xml, record) AS \\[record\\]
FROM sys.dm_os_ring_buffers
WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
AND record LIKE '%%<SystemHealth>%%') AS x
) AS y
ORDER BY record_id DESC;
我收到一个错误,说语法错误。我将此存储过程读入 NSString 对象,然后将 NSString 发送到 Web 服务,如下所示:
NSError *error;
NSString *txtFilePath = [[NSBundle mainBundle] pathForResource:@"Proc_Usage" ofType:@"txt"];
NSString *procUsage = [NSString stringWithContentsOfFile:txtFilePath encoding:NSUTF8StringEncoding error:&error];
if (procUsage == nil) {
return -1;
}
[[DbConnect DbConnect_Singleton] executeInDb:procUsage];