I need to add a concatenated field to my SELECT clause based on the results of my WHERE clause. Here is my current query:
Declare
#Low numeric(13,0) = 10000,
#High numeric(13,0) = 100000000000,
#Name varchar(100) = '%',
#Stname varchar(100) = '%georgia',
#Sumlev varchar(3) = 1,
#County varchar(30) = 123456,
#Place varchar (5) = 8,
#Gid2 varchar(5),
#Gid2_1 varchar (7);
if object_id('tempdb..#GeoID2') is not null drop table #GeoID2
SELECT
SUMLEV, State, County, Place, Name, Stname, ESTIMATESBASE2010, CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 3 - LEN(COUNTY)) + COUNTY) AS Gid2
INTO
#GeoID2
FROM
[CensusData].[dbo].[SUB-EST2014_ALL]
WHERE
(NOT (SUMLEV = #Sumlev)) AND (NOT (County = #County)) AND (NOT (PLACE = #Place)) AND
(ESTIMATESBASE2010 > #Low) AND (ESTIMATESBASE2010 < #High) AND (Name LIKE #Name) and (Stname LIKE #Stname)
SELECT
[#GeoID2].SUMLEV, [#GeoID2].State, [#GeoID2].County, [#GeoID2].Place, [#GeoID2].Stname, [#GeoID2].Name, [#GeoID2].ESTIMATESBASE2010, [ACS_14_5YR_S1901_with_ann].[HC01_EST_VC13] as Avg_Salary, [#GeoID2].Gid2, [#GeoID2].Gid21
FROM
[CensusData].[dbo].[#GeoID2] INNER JOIN
[CensusData].[dbo].[ACS_14_5YR_S1901_with_ann] ON [CensusData].[dbo].[#GeoID2].Gid2 = [CensusData].[dbo].[ACS_14_5YR_S1901_with_ann].[GEO id2]
ORDER BY
[#GeoID2].NAME
I need to add a CASE statement to my first SELECT clause to assign a value to Gid2. The current value is assigned as:
CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 3 - LEN(COUNTY)) + COUNTY) AS Gid2
What I need to do is check the value of [County] before I assign this value. Based on that result I will use one of two formulas. Here is what I tried (that does not work):
Case
When (County = 0)
Then 'CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 5 - LEN(PLACE)) + PLACE) AS Gid2'
Else 'CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 3 - LEN(COUNTY)) + COUNTY) AS Gid2'
End
I am not sure that using CASE is the right way to go, but I can't figure out any other way to approach this. Can someone help?
Ok I figured it out. I was using ' in front of my Concat function when I should have been using (. Here is a working query:
Declare
#Low numeric(13,0) = 10000,
#High numeric(13,0) = 100000000000,
#Name varchar(100) = '%',
#Stname varchar(100) = '%georgia',
#Sumlev varchar(3) = 1,
#County varchar(30) = 123456,
#Place varchar (5) = 8,
#Gid2 varchar(5),
#Gid2_1 varchar (7);
if object_id('tempdb..#GeoID2') is not null drop table #GeoID2
SELECT
SUMLEV, State, County, Place, Name, Stname, ESTIMATESBASE2010,
Case
When (County = 0)
Then (CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 5 - LEN(PLACE)) + PLACE))
Else (CONCAT(REPLICATE('0', 2 - LEN(STATE)) + STATE, REPLICATE('0', 3 - LEN(COUNTY)) + COUNTY))
End AS Gid2
INTO
#GeoID2
FROM
[CensusData].[dbo].[SUB-EST2014_ALL]
WHERE
(NOT (SUMLEV = #Sumlev)) AND (NOT (County = #County)) AND (NOT (PLACE = #Place)) AND
(ESTIMATESBASE2010 > #Low) AND (ESTIMATESBASE2010 < #High) AND (Name LIKE #Name) and (Stname LIKE #Stname)
SELECT
[#GeoID2].SUMLEV, [#GeoID2].State, [#GeoID2].County, [#GeoID2].Place, [#GeoID2].Stname, [#GeoID2].Name, [#GeoID2].ESTIMATESBASE2010, [ACS_14_5YR_S1901_with_ann].[HC01_EST_VC13] as Avg_Salary, [#GeoID2].Gid2
FROM
[CensusData].[dbo].[#GeoID2] INNER JOIN
[CensusData].[dbo].[ACS_14_5YR_S1901_with_ann] ON [CensusData].[dbo].[#GeoID2].Gid2 = [CensusData].[dbo].[ACS_14_5YR_S1901_with_ann].[GEO id2]
ORDER BY
[#GeoID2].NAME
Related
I am looking to enhance the web interface of my SSRS instance (2016). Are there any options to use tabs or a left hand report hierarchical tree to navigate between the reports?
Currently all I can see is the ability to go up a level then be presented with a list of the reports available in that level.
Are there any other options or generally accepted solutions for better navigation within SSRS?
One thing that caught my eye was document maps. Is there a way to create a document map which enables report navigation? Not navigation within a report, but navigation to another report?
Thanks
Mike
I use a report to view all the deployed reports on the report server. It could get you part of the way there. The icons are links to folders/reports. I also show the execution count and subscription count. The XML code exceeds the limit in this answer. Here's a shortcut to the XML/rdl file in GitHub. Copy and paste the XML into a text file then rename the text file with .rdl extension.
Screenshot of report preview
SQL for the report
/*
+-----------------------------------------------------------------------------
| Purpose: To search deployed reports on the report server
| Note: SQLCmdMode Script (from the SSMS menu [Query]|[SQLCMD Mode])
+-----------------------------------------------------------------------------
:setvar _server "YourServerNameHere"
:setvar _database "ReportServer"
--:setvar _user "***username***"
--:setvar _password "***password***"
--:connect $(_server) -U $(_user) -P $(_password)
:connect $(_server)
USE [$(_database)];
GO
DECLARE #ReportFolder AS VARCHAR(100)
DECLARE #ReportName AS VARCHAR(100)
DECLARE #ReportDescription AS VARCHAR(50)
DECLARE #CreatedBy AS VARCHAR(50)
DECLARE #CreatedDate AS DATETIME
DECLARE #ModifiedBy AS VARCHAR(50)
DECLARE #ModifiedDate AS DATETIME
DECLARE #ReportDefinition AS VARCHAR(50)
DECLARE #SearchFor AS VARCHAR(50)
DECLARE #SearchType AS VARCHAR(50)
DECLARE #all_value AS VARCHAR(50)
SET #ReportFolder = '<ALL>'
SET #ReportName = NULL
SET #ReportDescription = NULL
SET #CreatedBy = NULL
SET #CreatedDate = NULL
SET #ModifiedBy = NULL
SET #ModifiedDate = NULL
SET #ReportDefinition = NULL
SET #SearchFor = NULL
SET #SearchType = NULL
SET #all_value = '<ALL>'
*/
;WITH
report_users
AS
(
SELECT [UserID], [SimpleUserName] = UPPER(RIGHT([UserName], (LEN([UserName])-CHARINDEX('\', [UserName])))) FROM dbo.[Users]
)
,
report_catalog
AS
(
SELECT
rpt.[ItemID]
, rpt.[CreatedById]
, rpt.[ModifiedById]
, rpt.[Type]
, rpt.[Name]
, rpt.[Description]
, rpt.Parameter
, [CreationDate] = CONVERT(DATETIME, CONVERT(VARCHAR(11), rpt.[CreationDate], 13))
, [ModifiedDate] = CONVERT(DATETIME, CONVERT(VARCHAR(11), rpt.[ModifiedDate], 13))
, [ReportFolder] = SUBSTRING(rpt.[Path], 2, Len(rpt.[Path])-Len(rpt.[Name])-2)
, rpt.[Path]
, [URL_ReportFolder] = 'http://' + Host_Name() + '/Reports/browse/' + SUBSTRING(rpt.[Path], 2, Len(rpt.[Path])-Len(rpt.[Name])-2)
, [URL_Report] = 'http://' + Host_Name() + '/Reports/report/' + SUBSTRING(rpt.[Path], 2, Len(rpt.[Path])-Len(rpt.[Name])-2) + '%2f' + rpt.[Name]
, [ReportDefinition] = CONVERT(VARCHAR(MAX), CONVERT(VARBINARY(MAX), rpt.[Content]))
FROM
dbo.[Catalog] AS rpt
WHERE
1=1
AND rpt.[Type] = 2
)
SELECT
rpt.ItemID
, rpt.[Name]
, rpt.[Description]
, rpt.[Parameter]
, [ReportCreatedBy] = urc.[SimpleUserName]
, [ReportCreationDate] = rpt.[CreationDate]
, [ReportModifiedBy] = urm.[SimpleUserName]
, [ReportModifiedDate] = rpt.[ModifiedDate]
, rpt.[ReportFolder]
, [ReportPath] = rpt.[Path]
, rpt.[URL_ReportFolder]
, rpt.[URL_Report]
, rpt.[ReportDefinition]
, [CommandText] = rpt.[ReportDefinition]
, el.[ExecutionLogCount]
, sc.[SubscriptionCount]
, [SearchForStr] = ISNULL(#SearchFor, ' ')
FROM
report_catalog AS rpt
LEFT JOIN (SELECT [ExecutionLogCount] = COUNT([ReportID]), [ReportID] FROM dbo.[ExecutionLog] GROUP BY [ReportID]) el ON el.[ReportID] = rpt.[ItemID]
LEFT JOIN (SELECT [SubscriptionCount] = COUNT([Report_OID]), [Report_OID] FROM dbo.[Subscriptions] GROUP BY [Report_OID]) sc ON sc.[Report_OID] = rpt.[ItemID]
LEFT JOIN report_users AS urc ON rpt.[CreatedById] = urc.[UserID]
LEFT JOIN report_users AS urm ON rpt.[ModifiedById] = urm.[UserID]
WHERE
1=1
AND (#all_value IN (#ReportFolder) OR rpt.[ReportFolder] IN(#ReportFolder))
AND (#CreatedBy IS NULL OR urc.[SimpleUserName] LIKE '%' + #CreatedBy + '%')
AND (#CreatedDate IS NULL OR rpt.[CreationDate] >= #CreatedDate)
AND (#ModifiedBy IS NULL OR urm.[SimpleUserName] LIKE '%' + #ModifiedBy + '%')
AND (#ModifiedDate IS NULL OR rpt.[ModifiedDate] >= #ModifiedDate)
AND (
#SearchFor IS NULL
OR (
(rpt.[Name] LIKE '%' + #SearchFor + '%' AND (#all_value IN(#SearchType) OR 'Report Name' IN(#SearchType)))
OR (rpt.[Description] LIKE '%' + #SearchFor + '%' AND (#all_value IN(#SearchType) OR 'Report Description' IN(#SearchType)) )
OR (PATINDEX('%' + #SearchFor + '%', rpt.[ReportDefinition]) > 0 AND (#all_value IN(#SearchType) OR 'Report Definition' IN(#SearchType)) )
)
)
Hi I am facing one problem,
I have a situation that I need to select data from db based on priority of matched columns in where clause
ex. I have a table
Employee: FirstName, LastName, City, State.
I want to search employee's records based on criteria. but the situation is that first of all I need to get those records where all the four criteria (columns) matched, then all those records where three of four criteria(Columns) matched, then all those records where two of four criteria (Column) matched so on..
For that I am creating dynamic query based on situations, but if search criteria will extend more, than I need to put many conditions in query it will difficult to manage the dynamic query. So is there any other way to handle this problem?
Many Thanks in advance.
This has been tested on SQL Server but should give you an idea on MySQL if it doesn't work right away;
Test Data
IF OBJECT_ID('tempdb..#TestData') IS NOT NULL DROP TABLE #TestData
GO
CREATE TABLE #TestData (FirstName varchar(20), LastName varchar(20), City varchar(20), State varchar(20))
INSERT INTO #TestData (FirstName, LastName, City, State)
VALUES
('Jon','Snow','Winterfell','Westeros')
,('Jaime','Lannister','Riverrun','Westeros')
,('Margaery','Tyrell','Kings Landing','Westeros')
,('Cersei','Lannister','Kings Landing','Westeros')
,('Daenerys','Targaryen','Mereen','Essos')
,('Davos','Seaworth','Winterfell','Westeros')
,('Theon','Greyjoy','Mereen','Essos')
,('Tyrion','Lannister','Mereen','Essos')
,('Petyr','Baelish','Winterfell','Westeros')
Query with the search criteria set as variables. The case will check how many matches we have and order by that field.
DECLARE #Variable1 varchar(20); SET #Variable1 = 'Jon'
DECLARE #Variable2 varchar(20); SET #Variable2 = 'Snow'
DECLARE #Variable3 varchar(20); SET #Variable3 = 'Winterfell'
DECLARE #Variable4 varchar(20); SET #Variable4 = 'Westeros'
SELECT
FirstName
,LastName
,City
,State
,CASE WHEN FirstName = #Variable1 THEN 1 ELSE 0 END +
CASE WHEN LastName = #Variable2 THEN 1 ELSE 0 END +
CASE WHEN City = #Variable3 THEN 1 ELSE 0 END +
CASE WHEN State = #Variable4 THEN 1 ELSE 0 END Matches
FROM #TestData
ORDER BY (
CASE WHEN FirstName = #Variable1 THEN 1 ELSE 0 END +
CASE WHEN LastName = #Variable2 THEN 1 ELSE 0 END +
CASE WHEN City = #Variable3 THEN 1 ELSE 0 END +
CASE WHEN State = #Variable4 THEN 1 ELSE 0 END
) DESC
Which gives the result as;
FirstName LastName City State Matches
Jon Snow Winterfell Westeros 4
Davos Seaworth Winterfell Westeros 2
Petyr Baelish Winterfell Westeros 2
Jaime Lannister Riverrun Westeros 1
Margaery Tyrell Kings Landing Westeros 1
Cersei Lannister Kings Landing Westeros 1
Daenerys Targaryen Mereen Essos 0
Theon Greyjoy Mereen Essos 0
Tyrion Lannister Mereen Essos 0
You can use your conditions as +1 to rank your record.
http://sqlfiddle.com/#!9/f65790/3
SELECT FirstName, LastName, City, State
FROM Employee
ORDER BY (FirstName = 'John')
+ (LastName='Snow')
+ (City='Winterfell')
+ (State='Nord') DESC
Or if you need to check that rank somewhere downthere:
SELECT FirstName, LastName, City, State,
(FirstName = 'John')
+ (LastName='Snow')
+ (City='Winterfell')
+ (State='Nord') Rank
FROM employee
ORDER BY Rank DESC;
How to remove a character after the character ^ from a selected rows in table?
e.g.
TABLE Things
Boat
Do^2gs
Cat^fs
^KBear
Mi^&ce
D^Rice
RESULTS:
Boat
Dogs
Cats
Bear
Mice
Dice
select case when charindex('^', col) <> 0
then stuff(col, charindex('^', col), 2, '')
else col
end
-- to handle multiple ^ up to max of 4
select t.col,
r4.col
from Things t
cross apply
(
select col = case when charindex('^', col) <> 0
then stuff(col, charindex('^', col), 2, '')
else col
end
) r1
cross apply
(
select col = case when charindex('^', r1.col) <> 0
then stuff(r1.col, charindex('^', r1.col), 2, '')
else r1.col
end
) r2
cross apply
(
select col = case when charindex('^', r2.col) <> 0
then stuff(r2.col, charindex('^', r2.col), 2, '')
else r2.col
end
) r3
cross apply
(
select col = case when charindex('^', r3.col) <> 0
then stuff(r3.col, charindex('^', r3.col), 2, '')
else r3.col
end
) r4
-- UDF to remove the ^
create function remove_chr
(
#str varchar(100)
)
returns varchar(100)
as
begin
while charindex('^', #str) <> 0
begin
select #str = case
when charindex('^', #str) <> 0
then stuff(#str, charindex('^', #str), 2, '')
else #str
end
end
return #str
end
If you are using MySQL you could use:
SELECT col,
IF(INSTR(col,'^') > 0,CONCAT(LEFT(col,INSTR(col, '^')-1),
RIGHT(col,LENGTH(col) - INSTR(col, '^')-1)), col) AS result
FROM Things;
SqlFiddleDemo
And SQL Server equivalent:
SELECT col,
IIF(CHARINDEX('^',col) > 0,CONCAT(LEFT(col,CHARINDEX('^',col)-1),
RIGHT(col,LEN(col) - CHARINDEX('^',col)-1)), col) AS result
FROM Things
LiveDemo
SQL Server 2008:
SELECT col,
CASE WHEN CHARINDEX('^',col) > 0
THEN LEFT(col,CHARINDEX('^',col)-1) + RIGHT(col,LEN(col) - CHARINDEX('^',col)-1)
ELSE col
END AS result
FROM Things;
Keep in mind that it will work only if there is none or one occurence of ^.
Here is the solution which removes any number of occurrence of '^' .
I have created a function SQL server which is not used any loop or cursor.
CREATE FUNCTION [dbo].[FnReplaceChar](#pOriginalText VARCHAR(2000))
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE #vText VARCHAR(1000)
,#vXML XML
--Convert text as XML format
SELECT #vXML = '<Root><dtl><f>' + REPLACE(#pOriginalText,'^','</f></dtl><dtl><f>^')+'</f></dtl></Root>'
--Splits words started with '^' and combines after removing character starts with '^'
SET #vText = (
SELECT '' + ACT_TEXT
FROM
(
SELECT CASE WHEN CHARINDEX('^',DOC.COL.value('f[1]','VARCHAR(100)') ,0) > 0
THEN STUFF(DOC.COL.value('f[1]','VARCHAR(100)'),CHARINDEX('^',DOC.COL.value('f[1]','VARCHAR(100)') ,0),2,'')
ELSE DOC.COL.value('f[1]','VARCHAR(100)')
END AS ACT_TEXT
FROM #vXML.nodes('/Root/dtl') DOC(COL)
)T
FOR XML PATH('')
)
RETURN #vText
END
You can use this function in your select query
SELECT dbo.[FnReplaceChar](col_Name)
FROM [Things]
I have definition column of a medication. I want the definition to be split into [product name(PN)], [doseform(DF)], [total dose(TD)] and [units].
Eg:
BENADRYL: MYLANTA 1:1 SOLUTION(Benadryl mylanta(PN),Sol(DF),1:1(TD),NULL(units))
MASK AND SPACER (Mark and Spacer(PN),NUll,NUll,NUll)
BL VITAMIN B-6 50 MG TABS(BL Vitamin(PN),Tabs(DF),50(TD),MG(Units))
I've made a few assumptions about your data. 1 is that the first opening bracket begins the definition ie. there are no brackets in the name before the definition. I am also assuming the the product definition structure will not change.
So, to get to your answer, I have used a modified split string function. I used this one as a base: T-SQL split string
The difference here is that the function will return a 2 column table so we can identify the definition parts
CREATE FUNCTION dbo.splitstring_custom
(
#stringToSplit VARCHAR(MAX)
)
RETURNS #returnList TABLE
(
[Type] [nvarchar](20)
,[Name] [nvarchar](500)
)
AS
BEGIN
DECLARE #name NVARCHAR(255)
DECLARE #pos INT
DECLARE #colidx INT = 1
WHILE CHARINDEX(',', #stringToSplit) > 0
BEGIN
SELECT #pos = CHARINDEX(',', #stringToSplit)
SELECT #name = SUBSTRING(#stringToSplit, 1, #pos - 1)
INSERT INTO #returnList
SELECT CASE WHEN #colidx = 1 THEN 'Product Name'
WHEN #colidx = 2 THEN 'Dose Form'
WHEN #colidx = 3 THEN 'Total Dose'
WHEN #colidx = 4 THEN 'Units'
END
,#name
SELECT #stringToSplit = SUBSTRING(#stringToSplit, #pos + 1, LEN(#stringToSplit) - #pos)
SET #colidx += 1
END
INSERT INTO #returnList
SELECT CASE WHEN #colidx = 1 THEN 'Product Name'
WHEN #colidx = 2 THEN 'Dose Form'
WHEN #colidx = 3 THEN 'Total Dose'
WHEN #colidx = 4 THEN 'Units'
END
,#stringToSplit
RETURN
END
Then we get the relevant part of the definition using some string manipulation:
SUBSTRING(medication, CHARINDEX('(', medication) + 1, LEN(medication) - (CHARINDEX('(', medication) + 1))
Then PIVOT the results to flatten it out:
DECLARE #t TABLE (id varchar(20), medication varchar(100))
INSERT INTO #t VALUES ('BENADRYL', 'BENADRYL: MYLANTA 1:1 SOLUTION(Benadryl mylanta(PN),Sol(DF),1:1(TD),NULL(units))')
INSERT INTO #t VALUES ('MASK', 'MASK AND SPACER (Mark and Spacer(PN),NULL,NULL,NULL)')
INSERT INTO #t VALUES ('BL VITAMIN', 'BL VITAMIN B-6 50 MG TABS(BL Vitamin(PN),Tabs(DF),50(TD),MG(Units))')
SELECT id, [Product Name], [Dose Form], [Total Dose], [Units]
FROM (SELECT id, Type, Name
FROM #t t
CROSS APPLY dbo.splitstring_custom(SUBSTRING(medication, CHARINDEX('(', medication) + 1, LEN(medication) - (CHARINDEX('(', medication) + 1)))
) X PIVOT ( MAX(Name) FOR TYPE IN ([Product Name], [Dose Form], [Total Dose], [Units]) ) pvt
HI i have two tables in my database named...Requests and Balance tracker which has no relation....but i want to select data from two tables and binf it two grid...
Requests
EmpID |EmpRqsts|EmpDescription|ApproverID|ApprovedAmount|RequestPriority
1 |asdfsb |sadbfsbdf |1 |
2 |asbfd |sjkfbsd |1 |
Balance Tracker
EmpId|BalanceAmnt|LastUpdated|lastApprovedAmount
| 1 |5000 |sdfbk |
| 2 |3000 |sjbfsh |
now i want to update based on the EmpID two tables at a time...when ever amount is approved it should be updates in request table column [ApprovedAmount] and with priority...
when [ApprovedAmount] is Updated [BalanceAmnt] Balance Tracker of also should be Updated by adding the amount approved,[LastUpdated],[lastApprovedAmount] should be updated with date and time
can any one help me with the query please....
#Anil, here is an example of SQL Server 2008 code which would help you to get your goal acomplished:
DECLARE #Requests TABLE
(
EmpId int
, EmpRqsts nvarchar(50)
, EmpDescription nvarchar(250)
, ApproverID int
, ApprovedAmount money
, RequestPriority int
)
DECLARE #BalanceTracker TABLE
(
EmpId int
, BalanceAmnt money
, LastUpdated datetime
, lastApprovedAmount money
)
-- Insert data for testing
INSERT INTO #Requests VALUES
(
1
, 'Something here'
, 'Some descriptio here'
, 1
, 100
, 1
)
INSERT INTO #Requests VALUES
(
2
, 'Something here 2 '
, 'Some descriptio here 3'
, 1
, 215
, 2
)
INSERT INTO #BalanceTracker VALUES
(
1
, 5000
, GETDATE() - 3
, 310
)
INSERT INTO #BalanceTracker VALUES
(
2
, 3000
, (GETDATE() - 1)
, 98
)
-- Declare local variables
DECLARE
#NewAmount money
, #NewPriority int
, #SelectedEmpId int
-- Assing values for example
SELECT #NewAmount = 1000
, #SelectedEmpId = 1
, #NewPriority = 5
-- Get the tables values pre - updates
SELECT *
FROM #Requests
SELECT *
FROM #BalanceTracker
BEGIN TRY
-- Update the record with new ApprovedAmount and Request Priority
UPDATE #Requests
SET ApprovedAmount = #NewAmount
, RequestPriority = #NewPriority
WHERE EmpId = #SelectedEmpId
-- If no error found then update BalanceAmnt trable
IF (##ERROR = 0)
BEGIN TRY
UPDATE #BalanceTracker
SET BalanceAmnt = (BalanceAmnt + #NewAmount)
, LastUpdated = GETDATE()
, lastApprovedAmount = #NewAmount
WHERE EmpId = #SelectedEmpId
END TRY
BEGIN CATCH
PRINT N'Error found updating #BalanceTracker table: ' + ISNULL(LTRIM(STR(ERROR_NUMBER())) , N'Unknown Error' )
+ N', Message: ' + ISNULL ( ERROR_MESSAGE() , N'No Message' )
END CATCH
END TRY
BEGIN CATCH
PRINT N'Error found updating #Requests table: ' + ISNULL(LTRIM(STR(ERROR_NUMBER())) , N'Unknown Error' )
+ N', Message: ' + ISNULL ( ERROR_MESSAGE() , N'No Message' )
END CATCH
-- Get the tables values post - updates
SELECT *
FROM #Requests
SELECT *
FROM #BalanceTracker
Note 1: #Table are Variable Tables handlded by SQL Server 2008. If you're using previous version you should be able to create Temporary Table (#Table).
Note 2: data data-types may vary depending upon the SQL version you're using.
You could do this type of thing with a trigger. This way whenever you do the first update, it will automatically do the other update you specify.