Invalid object name with #tem_table - sql

I am trying to execute a query to get highCpuUtilization queries alert. It is displaying results, if I am executing the query. When I want to send the results to my mail using dbmail, it is throwing error.
(3 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
Msg 22050, Level 16, State 1, Line 0 Error formatting query, probably
invalid parameters Msg 14661, Level 16, State 1, Procedure
sp_send_dbmail, Line 517 Query execution failed: Msg 208, Level 16,
State 1, Server FSQADBTRVS\FSQADBTR, Line 1 Invalid object name
'#PossibleCPUUtilizationQueries'.
Here is the Query, That I am trying to Run
DECLARE #ts_now bigint
DECLARE #SQLVersion decimal (4,2) -- 9.00, 10.00
DECLARE #AvgCPUUtilization DECIMAL(10,2)
SELECT #SQLVersion = LEFT(CAST(SERVERPROPERTY('PRODUCTVERSION') AS VARCHAR), 4) -- find the SQL Server Version
-- sys.dm_os_sys_info works differently in SQL Server 2005 vs SQL Server 2008+
-- comment out SQL Server 2005 if SQL Server 2008+
-- SQL Server 2005
--IF #SQLVersion = 9.00
--BEGIN
-- SELECT #ts_now = cpu_ticks / CONVERT(float, cpu_ticks_in_ms) FROM sys.dm_os_sys_info
--END
-- SQL Server 2008+
IF #SQLVersion >= 10.00
BEGIN
SELECT #ts_now = cpu_ticks/(cpu_ticks/ms_ticks) FROM sys.dm_os_sys_info
END
-- load the CPU utilization in the past 3 minutes into the temp table, you can load them into a permanent table
SELECT TOP(3) SQLProcessUtilization AS [SQLServerProcessCPUUtilization]
,SystemIdle AS [SystemIdleProcess]
,100 - SystemIdle - SQLProcessUtilization AS [OtherProcessCPU Utilization]
,DATEADD(ms, -1 * (#ts_now - [timestamp]), GETDATE()) AS [EventTime]
INTO #CPUUtilization
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
-- check if the average CPU utilization was over 90% in the past 2 minutes
SELECT #AvgCPUUtilization = AVG([SQLServerProcessCPUUtilization] + [OtherProcessCPU Utilization])
FROM #CPUUtilization
WHERE EventTime > DATEADD(MM, -2, GETDATE())
IF #AvgCPUUtilization >= 0
BEGIN
SELECT TOP(10)
CONVERT(VARCHAR(25),#AvgCPUUtilization) +'%' AS [AvgCPUUtilization]
, GETDATE() [Date and Time]
, r.cpu_time
, r.total_elapsed_time
, s.session_id
, s.login_name
, s.host_name
, DB_NAME(r.database_id) AS DatabaseName
, SUBSTRING (t.text,(r.statement_start_offset/2) + 1,
((CASE WHEN r.statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), t.text)) * 2
ELSE r.statement_end_offset
END - r.statement_start_offset)/2) + 1) AS [IndividualQuery]
, SUBSTRING(text, 1, 200) AS [ParentQuery]
, r.status
, r.start_time
, r.wait_type
, s.program_name
INTO #PossibleCPUUtilizationQueries
FROM sys.dm_exec_sessions s
INNER JOIN sys.dm_exec_connections c ON s.session_id = c.session_id
INNER JOIN sys.dm_exec_requests r ON c.connection_id = r.connection_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE s.session_id > 50
AND r.session_id != ##spid
order by r.cpu_time desc
-- query the temp table, you can also send an email report
SELECT * FROM #PossibleCPUUtilizationQueries
END
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Databasemail',
#recipients = 'con-balususr#mail.com',
#query = 'SELECT * FROM #PossibleCPUUtilizationQueries',
#subject = 'Work Order Count',
#attach_query_result_as_file = 1 ;
-- drop the temp tables
IF OBJECT_ID('TEMPDB..#CPUUtilization') IS NOT NULL
drop table #CPUUtilization
IF OBJECT_ID('TEMPDB..#PossibleCPUUtilizationQueries') IS NOT NULL
drop table #PossibleCPUUtilizationQueries

EXEC msdb.dbo.sp_send_dbmail will run in a different context and has no permissions to your declared temporary table.
Use a global temporary table instead (##PossibleCPUUtilizationQueries).
As Aaron quite rightly pointed out you could also just use a permanent table to store your data rather than use temporary tables.
See here for more information regarding local and global temporary tables
There are two types of temporary tables: local and global. Local
temporary tables are visible only to their creators during the same
connection to an instance of SQL Server as when the tables were first
created or referenced. Local temporary tables are deleted after the
user disconnects from the instance of SQL Server. Global temporary
tables are visible to any user and any connection after they are
created, and are deleted when all users that are referencing the table
disconnect from the instance of SQL Server.

Related

How to create stored procedure with view?

I tried to execute this code and it worked o.k without the the stored procedure and with it, it made an error.
The error is:
Msg 102, Level 15, State 1, Procedure UV_MTBF, Line 251
Incorrect syntax near 'Event_'.
Is the stored procedure have a limitation in length?
can someone help me with my code?
edit*
my problem is with ' + QUOTENAME(#category,N'''') + N'
i want to add an integer from a variable that i received in the stored procedure. how can i do it?
enter code here:
CREATE PROCEDURE dbo.MTBFCalculation #Category int, #Action bit, #relateToParent bit
as
IF EXISTS (SELECT 1 FROM sys.objects WHERE [name] = '[dbo].[UV_MTBF]')
DROP VIEW [dbo].[UV_MTBF];
DECLARE #Event nvarchar(MAX) = N'
CREATE VIEW [dbo].[UV_MTBF]
as
with failureReportTable as (SELECT [ID] as failure_id
,[Login_ID]
,[Event_ID]
,[StartDate]
,[EndDate]
,DATEDIFF(Hour,[StartDate],[EndDate]) as eventDurationMin
,[IsRelevantForBI]
,[IsParallelReport]
,[ParentReportID]
,[IsPausedEvent]
,Case
When ParentReportID>0 Then 1 --Chiled
When IsParallelReport=1 Then 2 --Parent
Else 3 --not Parallel
End as ParallelStatus
FROM [TDM_Analysis].[dbo].[FailureReports]),
fullFailure as (select *, ROW_NUMBER() OVER (ORDER BY [StartDate] ) AS IDrow
from failureReportTable join [TDM_Analysis].[dbo].[UV_filteredLogins] as viewLogins on failureReportTable.Login_ID=viewLogins.ID
WHERE event_id IN (SELECT ID FROM [TDM_Analysis].[dbo].[Events] where EventCategory_ID=' + QUOTENAME(#category,N'''') + N')
and (ParallelStatus=3 or ParallelStatus=(case when ' + QUOTENAME(#relateToParent,N'''') + N'=1 then 2 else 1 end))),
--------------create first failure table------------------
failure_Event_1 as (select f1.failure_id as Event_1_Failure_ID
,f1.[Login_ID] as Event_1_Login_ID
,f1.[Event_ID] as Event_1_Event_ID
,f1.[StartDate] as Event_1_StartDate
,f1.[EndDate] as Event_1_EndDate
,f1.eventDurationMin as Event_1_eventDurationMin
--,f1.[IsRelevantForBI] as Event_1_IsRelevantForBI
--,f1.[IsParallelReport] as Event_1_IsParallelReport
-- ,f1.[ParentReportID] as Event_1_ParentReportID
-- ,f1.[IsPausedEvent] as Event_1_IsPausedEvent
,f1.[Test_Name] as Event_1_TestName
,f1.Phase_Name as Event_1_PhaseName
,f1.PressName as Event_1_PressName
,f1.PressType as Event_1_PressType
--,f1.[Operator] as Event_1_Operator
,f1.[LoginDate] as Event_1_LoginDate
,f1.[LogoutDate] as Event_1_LogoutDate
,f1.TimeDiff as Event_1_LoginDuration
,f1.IDrow+1 as row1
from fullFailure as f1),
--------------create second failure table------------------
failure_Event_2 as (select f1.failure_id as Event_2_Failure_ID
,f1.[Login_ID] as Event_2_Login_ID
,f1.[Event_ID] as Event_2_Event_ID
,f1.[StartDate] as Event_2_StartDate
,f1.[EndDate] as Event_2_EndDate
,f1.eventDurationMin as Event_2_eventDurationMin
-- ,f1.[IsRelevantForBI] as Event_2_IsRelevantForBI
-- ,f1.[IsParallelReport] as Event_2_IsParallelReport
-- ,f1.[ParentReportID] as Event_2_ParentReportID
-- ,f1.[IsPausedEvent] as Event_2_IsPausedEvent
,f1.[Test_Name] as Event_2_TestName
,f1.Phase_Name as Event_2_PhaseName
,f1.PressName as Event_2_PressName
,f1.PressType as Event_2_PressType
-- ,f1.[Operator] as Event_2_Operator
,f1.[LoginDate] as Event_2_LoginDate
,f1.[LogoutDate] as Event_2_LogoutDate
,f1.TimeDiff as Event_2_LoginDuration
,f1.IDrow as row2
from fullFailure as f1),
------------- join two failure tabels and calculating MTTR-mean time to repair (duration of failue), MTTF-mean time to failue( end of one until start of a new one), MTBF-mean time between failue (from start of a failure to start of a new one)--------------------
joinFailures as (select *, Event_1_eventDurationMin as MTTR
,CASE
When isnull(f2.row2,0)=0 then DATEDIFF(HOUR,f1.Event_1_EndDate,f1.Event_1_LogoutDate)
WHEN f1.Event_1_Login_ID=f2.Event_2_Login_ID THEN DATEDIFF(HOUR,f1.Event_1_EndDate,f2.Event_2_StartDate)
When (select TOP 1 sum(timediff)
from [TDM_Analysis].[dbo].[UV_filteredLogins]
where logindate>f1.Event_1_LogoutDate and logindate<f2.Event_2_LoginDate) is null then DATEDIFF(HOUR,f1.Event_1_EndDate,f1.Event_1_LogoutDate)+DATEDIFF(HOUR,f2.Event_2_LoginDate, f2.Event_2_StartDate)
ELSE
(select TOP 1 sum(timediff)+DATEDIFF(HOUR,f1.Event_1_EndDate,f1.Event_1_LogoutDate)+DATEDIFF(HOUR,f2.Event_2_LoginDate, f2.Event_2_StartDate)
from [TDM_Analysis].[dbo].[UV_filteredLogins]
where logindate>f1.Event_1_LogoutDate and logindate<f2.Event_2_LoginDate)
END AS MTTF
from failure_Event_1 as f1 left join failure_Event_2 as f2 on f1.row1=f2.row2),
positiveJoinFailure as (select * from joinFailures where MTTF>=0)
---- select calculation table order by ascending time----------
select * --Event_1_Failure_ID,Event_2_Failure_ID,MTTR,MTTF, MTTR+MTTF as MTFB
from positiveJoinFailure
--order by row1
';
--------------------------------------------------------Action------------------------------------------------------------------------------
if #Action=1
begin
EXEC sp_executesql #Event;
end
for this part of your query
where EventCategory_ID=' + QUOTENAME(#category,N'''') + N')
2 option here, you convert the value of #category to string and then concatenate with the dynamic query
where EventCategory_ID=' + convert(varchar(10), #category)
OR, you pass the value in as a parameter.
for this option, you specify #category in the dynamic query
where EventCategory_ID= #category
and (ParallelStatus=3 ....
and you pass the value in at sp_executesql
EXEC sp_executesql #Event, N'#category int', #category
By the way, Option 2 is the preferred method when using dynamic query

Converting SQL Server Query to Oracle

I have the SQL Server query shown below and I am trying to get it to work on Oracle. I have done some looking but am new to Oracle so I am not even sure what I should be looking for. I am hoping that I can make the a query to run adhoc not necessarily a procedure. The basic concept is the following:
Create a temporary table to hold data to be totaled.
Create a date table to get a list of dates.
Build a dynamic query to insert data into the temporary table.
Execute the dynamic query.
Select the summary from the temporary table.
Drop the temporary table
Please remember, I am new to Oracle and I have done some looking. I know that the variables and aliases must be formatted differently and can get the date table but I am not sure the proper Oracle way to create and execute dynamic queries where the table name is the dynamic part. I can create the dynamic string with the correct table name but don't know how to execute it. I have seen some examples but none of them seem to make sense to me for what I am trying to do.
-- Oracle query for dates
with dt (d) as (
select last_day(add_months(sysdate,-2))+1 + rownum - 1
from all_objects
where rownum <= sysdate-last_day(add_months(sysdate,-2))+1+1
)
select 'insert into #tt (cnt, eem, ers, sts) (
select count(1), eem_id, ers_id, sts_id
from event_error' || to_char(D, 'ddmmyy') || ' eve
group by sts_id, eem_id, ers_id); ' "QRY"
from dt;
What I have done in the past is create a bash script which would do the looping through each date and then used the script to summarize the output. This time however I am trying to learn something and I know that there has to be a way to do this in SQL in Oracle.
I appreciate any help or assistance and hope I have explained this well enough.
-- Working SQL Server query
-- declare variables
declare #query varchar(max);
-- create temporary table
create table #tt(cnt int, eem int, ers int, sts int);
-- get a list of dates to process
with dt (d) as
(
select dateadd(month, datediff(month, 0, getdate())-1, 0) as d
union all
select dateadd(dd, 1, d)
from dt
where dateadd(dd, 1, d) <= getdate()
)
-- build the dynamic query
select distinct
#query = stuff ((select 'insert into #tt (cnt, eem, ers, sts) (
select count(1), eem_id, ers_id, sts_id
from event_error' + replace(convert(varchar(5), d, 103), '/', '') + right(year(d), 2) + ' (nolock) eve
group by sts_id, eem_id, ers_id); '
from dt for xml path, type).value(N'.[1]',N'nvarchar(max)')
, 1, 1, N'')
from dt;
-- to execute the dynamic query
execute (#query);
-- query the temporary table
select
[Stream] = sts.sts_name,
[Count] = sum(eve.cnt),
[Error Status] = ers.ers_name,
[Error Number] = eem.eem_error_no,
[Error Text] = eem.eem_error_text
from
#tt eve
inner join
event_error_message eem on eem.eem_id = eve.eem
inner join
error_status ers on ers.ers_id = eve.ers
inner join
stream_stage sts on sts.sts_id = eve.sts
group by
sts.sts_name, eem.eem_error_no, eem.eem_error_text, ers.ers_name
order by
sts.sts_name, eem.eem_error_no, ers.ers_name;
-- drop the temporary table
drop table #tt;
So as I expected, after fighting this all day and finally giving up and asking for help I have an answer. The query below works however if you have improvements or constructive criticism please share as I said, I am trying to learn.
-- create the temporary table
create global temporary table my_tt (
cnt number
, sts number
, eem number
, ers number
)
on commit delete rows;
declare
V_TABL_NM ALL_TABLES.TABLE_NAME%TYPE;
V_SQL VARCHAR2(1024);
begin
for GET_TABL_LIST in (
with dt (d) as (
select last_day(add_months(sysdate,-2))+1 + rownum -1
from all_objects
where rownum <= sysdate-last_day(add_months(sysdate,-2))
)
select 'event_error' || to_char(D, 'ddmmyy') TABLE_NAME from dt
) loop
V_TABL_NM := GET_TABL_LIST.TABLE_NAME;
V_SQL := 'insert into my_tt select count(1), sts_id, eem_id, ers_id from ' || V_TABL_NM || ' group by sts_id, eem_id, ers_id';
execute immediate V_SQL;
end loop;
end;
/
-- the slash is important for the above statement to complete
select
sts.sts_name "Stream"
, sum(eve.cnt) "Count"
, ers.ers_name "Error Status"
, eem.eem_error_no "Error Number"
, eem.eem_error_text "Error Text"
from my_tt eve
inner join event_error_message eem
on eem.eem_id = eve.eem
inner join error_status ers
on ers.ers_id = eve.ers
inner join stream_stage sts
on sts.sts_id = eve.sts
group by sts.sts_name, eem.eem_error_no, eem.eem_error_text, ers.ers_name
order by sts.sts_name, eem.eem_error_no, ers.ers_name;
-- drop the temporary table
drop table my_tt purge;

Stored procedure becomes slow every couple of days

I am facing an issue on SQL Server in which my stored procedure becomes slow after couple of days.
Below is the sample of my stored procedure.
Could this be a caching issue on the server side? Can I increase the server's cache size to resolve the problem?
Normally the stored procedure returns data in one second.
#START_VALUE int=null,
#END_VALUE int=null
#UID NVARCHAR(MAX)=null,
AS
BEGIN
SELECT
dbo.TABLE1.ID,
ROW_NUMBER() OVER (ORDER BY TABLE1.UPDATED_ON desc) AS RN,
CONVERT(VARCHAR(10), dbo.TABLE1.DATE, 101) AS TDATE,
CATEGORY = (
SELECT TOP 1 COLUMN1
FROM TABLE5 CT1
WHERE TABLE1.CATEGORY = CT1.CATEGORY_ID
),
TYPETEXT = (
SELECT TOP 1 COLUMN1
FROM TABLE6 CT1
WHERE TABLE1.TYPE = CT1.TYPE_ID
),
IMAGE = STUFF(( SELECT DISTINCT ',' + CAST(pm.C1 AS varchar(12))
FROM TABLE2 pm
WHERE pm.ID = TABLE1.ID AND pm.C1 IS NOT NULL AND pm.C1 <> ''
FOR XML PATH('')),
1, 1, '' ) INTO #tempRecords
FROM dbo.TABLE1
WHERE ((#UID is null OR dbo.TABLE1.ID = #UID )
ORDER BY TABLE1.UPDATED DESC
SELECT #count = COUNT(*) FROM #tempRecords;
SELECT *, CONVERT([int],#count) AS 'TOTAL_RECORDS'
FROM #tempRecords
WHERE #tempRecords.RN BETWEEN CONVERT([bigint], #START_VALUE) AND CONVERT([bigint], #END_VALUE)
END
GO
'
A few performance tips:
1) #UID is null OR dbo.TABLE1.ID = #UID --> this is bad because you'll have one execution plan when UID is null and when it's not. Build a dynamic sql query and you'll get 2 execution plans.
2) Update stats in a maintenance plan.
3) Check index fragmentation.
4) Try to do the same thing without using a temp table.
5) Try to avoid castings.

How to execute SQL statements saved in a table with T-SQL

Is it possible to execute a SQL statement Stored in a Table, with T-SQL?
DECLARE #Query text
SET #Query = (Select Query FROM SCM.dbo.CustomQuery)
The statements that are stored in the table are ad-hoc statements which could be SELECT TOP 100 * FROM ATable to more complex statements:
Select
J.JobName As Job,
JD.JobDetailJobStart AS StartDate,
JD.JobDetailJobEnd AS EndDate,
(
SELECT (DATEDIFF(dd, JD.JobDetailJobStart, JD.JobDetailJobEnd) + 1) -(DATEDIFF(wk, JD.JobDetailJobStart, JD.JobDetailJobEnd) * 2) -(CASE WHEN DATENAME(dw, JD.JobDetailJobStart) = 'Sunday' THEN -1 ELSE 0 END) -(CASE WHEN DATENAME(dw, JD.JobDetailJobEnd) = 'Saturday' THEN -1 ELSE 0 END)
) AS NumberOfWorkingDays,
JD.JobDetailDailyTarget AS DailyTarget,
JD.JobDetailWeeklyTarget AS WeeklyTarget,
JD.JobDetailRequiredQTY AS RequiredQuantity,
(
Select SUM(sJL.JobLabourQuantityEmployees) From JobLabour sJL
) AS NumberOfEmployees,
(
Select
SUM((sEM.EmployeeDesignationDefaultRate * sJL.JobLabourQuantityEmployees)*8)*(SELECT (DATEDIFF(dd, JD.JobDetailJobStart, JD.JobDetailJobEnd) + 1) -(DATEDIFF(wk, JD.JobDetailJobStart, JD.JobDetailJobEnd) * 2) -(CASE WHEN DATENAME(dw, JD.JobDetailJobStart) = 'Sunday' THEN -1 ELSE 0 END) -(CASE WHEN DATENAME(dw, JD.JobDetailJobEnd) = 'Saturday' THEN -1 ELSE 0 END))
from EmployeeDesignation sEM
Inner join JobLabour sJL on sJL.EmployeeDesignationID = sEM.EmployeeDesignationID
) AS FullEmployeeRate
from Job J
Inner Join JobDetail JD on JD.JobID = J.JobID
Inner Join JobLabour JL on JL.JobID = J.JobID
WHERE J.JobActive = 0
I want to execute the #Query Variable that I declared from T-SQL. Is this possible? (I am running a MSSQL 2005 enviroment)
You can use
EXECUTE sp_executesql #Query
to run your T-SQL
Here's a link to the MS docn for SQL Server 2005
http://msdn.microsoft.com/en-us/library/ms188001%28v=sql.90%29.aspx
The previous answer allows you to run one statement, and is valid. The question was on how to run SQL Statements stored in a table, which I took as more than one statement being executed. For this extra step, there is a while loop involved to iterate through each statement that need to be run.
-- Author: Chad Slagle
DECLARE #Table table (RID BIGINT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
SQLText NVARCHAR(MAX) )
DECLARE #StatementMax INT
,#statementMin INT
,#isTest TINYINT = 1
,#SQLStatement NVARCHAR(MAX)
-- Insert SQL Into Temp Table
INSERT INTO #table (SQLText)
VALUES ('SELECT ##Version');
INSERT INTO #table (SQLText)
VALUES ('SELECT SERVERPROPERTY(''ProductVersion'')')
-- Get your Iterator Values
SELECT #statementMAX = MAX(RID), #statementMIN = MIN(RID) FROM #table
IF #isTest = 1 BEGIN SELECT *, #statementMax AS MaxVal, #StatementMin AS MinVal FROM #Table END
-- Start the Loop
WHILE #StatementMax >= #statementMin
BEGIN
SELECT #SQLStatement = SQLText FROM #table WHERE RID = #statementMin -- Get the SQL from the table
IF #isTest = 1 BEGIN SELECT 'I am executing: ' + #SQLStatement AS theSqlBeingRun, GETDATE(), #statementMin, #StatementMax END
ELSE
BEGIN
EXECUTE sp_ExecuteSQL #SQLStatement -- Execute the SQL
END
DELETE FROM #table WHERE RID = #statementMin -- Delete the statement just run from the table
SELECT #statementMIN = MIN(RID) FROM #Table -- Update to the next RID
IF #isTest = 1 BEGIN SELECT * FROM #table END
END
In Summary, I created a temp table and put some SQL in it, using a IDENTITY (RID) field to provide an iterator for the while loop. Then ran the while loop. In the example, you should return two views of your SQL Version. I built this on 2k8, and I hope it helps someone out of a jam one day..
We use a much simpler approach. Store the scripts (raw sql or stored procedure calls) in a table with a column containing an identifying code for said script. Use placeholders in your script for parameters. Any scripts that are used a lot can be "keyed" in your app or web config file. If scripts need to be executed in a specific order, put an ordinal column in the table. The actual "script" can then be pulled into a c# list or array, passed to a database class library and executed accordingly. This gives you dynamic control over your SQL and allows you to make changes on the database side for said scripts without recompiling your main application.
TRUNCATE TABLE AllTableUnion
DECLARE #Query2 Nvarchar(MAX)
SET #Query2='SELECT * FROM #UnionString t1)A'
INSERT INTO AllTableUnion
EXEC(#Query2)
DECLARE #Query4 Nvarchar(MAX)=(SELECT Query FROM AllTableUnion)
EXECUTE sp_ExecuteSQL #Query4
I have a similar situation I just can't get to work. I'd like to create a table, where we have a customer number, and the business rules used to determine codes for a variety of items. The table psudo looks like this:
| CustomerNumber | RuleName | Rule |
----------------------------------------------------------------------
| 12345 |ShippingCharged | iif(id.ItemID in (1,2,3,4,5,6) AND
cod.Code IN (5,6,7,8),1,0)
| 99999 |ShippingCharged | iif(id.ItemID in (1,2,3,7,9,10) AND
cod.Code NOT IN (5,7,8),1,0)
I want to run a SELECT in the form:
SELECT CustomerNumber, RuleName, Rule as Value
FROM CustomerRules cr
JOIN CustomerData cd
ON cd.CustomerNumber = cd.CustomerNumber
JOIN ItemsData id
ON cd.ItemID = id.ItemID
JOIN CodesData cod
ON cd.Code = cod.Code
WHERE cr.RuleName = 'ShippingCharged'
To return The Customer Number, the name of the Rule used and the calculated value of the IIF statement. I am getting the text of the Rule calculation, rather than the calculated value.
I've tried various forms of DSQL and TSQL but can't seem to get the column to be treated as a function, just a text value from the table.
Any ideas?

Cannot specify an index or locking hint for a remote data source SQL Server 2005

I have this query which involves a table from another server:
select count(serialno), max(convert(varchar(10),Crtd_DateTime,101))
from tblSBox tsb (nolock)
inner join tblMBox tmb (nolock) on tsb.idMaster = tmb.idMaster
inner join [server_2].fdb.dbo.sns_in_out sio
on tsb.serialno = sio.lotsernbr and invtMult='-1' and RefNbr like 'I%'
where tmb.WO=34612
but this query is taking forever, and I cannot specify an index or locking hint for a remote data source, so I looked into the web and I found out that I can use this:
SELECT * FROM OpenQuery(server, 'Select * from sometable with (nolock)')
Which works fine, but as you can see I have a link between tsb.serialno and sio.lotsernbr, and when I place all that inside the '' like the following:
select serialno,
(SELECT *
FROM OpenQuery([server_2], 'select convert(varchar(10), Crtd_DateTime, 101)
from fdb.dbo.sns_in_out sio (nolock)
where (tsb.serialno = sio.lotsernbr
and invtMult=''-1'' and RefNbr like ''I%'')')) as crtDate
from tblSBox tsb (nolock)
inner join tblMBox tmb (nolock) on tsb.idMaster = tmb.idMaster
where tmb.WO=34612
I get this error:
[OLE/DB provider returned message: Deferred prepare could not be
completed.]
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "tsb.serialno" could not be bound.
Any idea of how to accomplish this?
If you want to run queries with (NOLOCK) on a remote server, create a stored procedure or view on the remote server and apply the locking hint there.
If you have no access to the remote server, you could pull the data first by executing something like this through a linked server (I'll assume your linked server is called server_2):
CREATE TABLE #loc (lotsernbr INT, dt DATETIME);
INSERT #loc
EXEC [server_2].fdb..sp_executesql 'SELECT lotsernbr, Crt_DateTime
FROM dbo.sns_in_out WITH (NOLOCK)
WHERE invtMult=''-1'' AND RefNbr LIKE ''I%'';';
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; -- much better than nolock on every table
SELECT tsb.serialno, crtDate = CONVERT(CHAR(10), loc.dt, 101)
FROM tblSBox AS tsb
INNER JOIN #loc AS loc
ON tsb.serialno = loc.lotsernbr
INNER JOIN tblMBox AS tmb
ON tsb.idMaster = tmb.idMaster
WHERE tmb.WO = 34612;
Now you can join against the local #temp table. The difference may be negligible but it may be significant. If this brings across a lot of rows and you only need a few, you may want to do it slightly differently: build your IN list dynamically and pass it to the other server, e.g.
DECLARE #sql NVARCHAR(MAX);
SET #sql = N'';
SELECT #sql = #sql + ',' + CONVERT(VARCHAR(12), serialno)
FROM tblSBox AS tsb
INNER JOIN tblMBox AS tmb
ON tsb.idMaster = tmb.idMaster
WHERE tmb.WO = 34612
GROUP BY serialno; -- to avoid dupes
SET #sql = N'SELECT serialno = lotsernbr, crtDate = CONVERT(CHAR(10), Crt_Datetime, 101)
FROM fdb.dbo.sns_in_out WITH (NOLOCK)
WHERE lotsernbr IN (' + STUFF(#sql, 1, 1, N'') + ');'
-- now you don't need the join, since you are only outputting
-- the two columns:
EXEC [server_2].fdb..sp_executesql #sql;