Invalid object using WITH common table expression - c#

I am trying to use With in stored procedure
USE [BusOprtn]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AddRepairedStock]
#RepairedItems [dbo].[RepairedItems] readonly
AS
BEGIN
declare #productNo bigint,#productManufacturer bigint
SET NOCOUNT ON;
begin try
begin transaction
UPDATE [BusOprtn].[dbo].[RepairItem] SET [ReturnedQuantity] = rdi.ReturnedQuantity,[AmountPaid] = rdi.AmountPaid,[ReturnDate] = rdi.ReturnDate from [BusOprtn].[dbo].[RepairItem] as ri inner join #RepairedItems as rdi on ri.id=rdi.id;
;with y as (
select [PartUsedId],rdi.[ReturnedQuantity] from [BusOprtn].[dbo].[RepairItem] as ri inner join #RepairedItems as rdi on ri.Id=rdi.id
), x as (
SELECT [PartNo] ,[ManufacturerId],[ReturnedQuantity] FROM [BusOprtn].[dbo].[PartUsed] as p inner join y
on p.id = y.PartUsedId
)
UPDATE [BusOprtn].[dbo].[ProductMaster] SET [RepairedStock] =( [RepairedStock]+x.[ReturnedQuantity]) from [BusOprtn].[dbo].[ProductMaster] as pm inner join x on x.[PartNo]=pm.Id;
UPDATE [BusOprtn].[dbo].[ProductStockManufacturer] SET [RepairedCurrentStock] = ([RepairedCurrentStock]+x.[ReturnedQuantity]) from [BusOprtn].[dbo].[ProductStockManufacturer] as pm inner join x on x.[PartNo]=pm.[ProductNo] and x.[ManufacturerId]= pm.[ManufacturerId];
commit transaction
end try
BEGIN CATCH
declare #ErrorMessage nvarchar(max), #ErrorSeverity
int, #ErrorState int;
select #ErrorMessage = ERROR_MESSAGE() + ' Line ' + cast
(ERROR_LINE() as nvarchar(5)), #ErrorSeverity = ERROR_SEVERITY(),
#ErrorState = ERROR_STATE();
rollback transaction;
raiserror (#ErrorMessage, #ErrorSeverity, #ErrorState);
END CATCH
END
When i execute above command it executes successfully. But when i try to call stored procedure at runtime it gives error
`Invalid object name 'x'.
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.`
Earlier problem was with sub query as it not allowed with "WITH" so tried using another temp table y but error continued. I refereed Allowed Items for allowed items in WITH.

The error comes from the second update statement where you are using the CTE x
The scope for a CTE is one statement so x is only visible for the first update.
To fix this you can either duplicate the code for the CTE before the second update or you can use a temp table (or table variable) to capture the output from x and use the temp table in both your update statements instead of the CTE.
With a different formatting of the code this is easy to see.
First update statement:
with y as
(
select [PartUsedId],
rdi.[ReturnedQuantity]
from [BusOprtn].[dbo].[RepairItem] as ri
inner join #RepairedItems as rdi
on ri.Id=rdi.id
), x as
(
SELECT [PartNo],
[ManufacturerId],
[ReturnedQuantity]
FROM [BusOprtn].[dbo].[PartUsed] as p
inner join y
on p.id = y.PartUsedId
)
UPDATE [BusOprtn].[dbo].[ProductMaster]
SET [RepairedStock] = ([RepairedStock]+x.[ReturnedQuantity])
from [BusOprtn].[dbo].[ProductMaster] as pm
inner join x
on x.[PartNo]=pm.Id;
Second update statement:
UPDATE [BusOprtn].[dbo].[ProductStockManufacturer]
SET [RepairedCurrentStock] = ([RepairedCurrentStock]+x.[ReturnedQuantity])
from [BusOprtn].[dbo].[ProductStockManufacturer] as pm
inner join x
on x.[PartNo]=pm.[ProductNo] and x.[ManufacturerId]= pm.[ManufacturerId];

You can try to use SQL Server Profiler (Service menu in MSSMS). There you can find what query exactly received. Maybe something is different.
Hope it will help.

Related

SQL Update in Batches using Table Type Parameters

I am trying to update a table using table type parameter. Currently I am using the below query. But I would like to perform update in batches having batch size as one of the parameters. Kindly help.
ALTER PROCEDURE UPDATEStatus
#Ids int ,
#numbers TypeofNumbers readonly,
#Status char(2),
#nname varchar(50),
AS
BEGIN
BEGIN TRY
update e
set
e.status = #Status,
e.user =#nname,
e.time = GETDATE()
from detailtable e
join #numbers en on en.ID =e.ID
where e.oddIDs = #Ids
I tried to do in a single update but I wanted to do in sets or batches one by one. say 100 records first and then next 100 records until all are done
You can use something like this to do your update in batches:
CREATE OR ALTER PROCEDURE UPDATEStatus
#Ids INT,
#numbers TypeOfNumbers READONLY,
#Status CHAR(2),
#nname VARCHAR(50)
AS
BEGIN
DECLARE #UpdatedRows INT;
DECLARE #Skip INT = 0;
DECLARE #BatchSize INT = 100;
WHILE ISNULL(#UpdatedRows, 1) > 0
BEGIN
WITH CTE
AS (SELECT *
FROM #numbers AS n
ORDER BY n.ID OFFSET #Skip * #BatchSize ROWS FETCH NEXT #BatchSize ROWS ONLY)
UPDATE e
SET
e.[Status] = #Status,
e.[User] = #nname,
e.[time] = GETDATE()
FROM CTE AS en
JOIN detailtable e ON en.ID = e.ID;
SET #UpdatedRows = ##ROWCOUNT;
SET #Skip = #Skip + 1;
END;
END;
GO
Next time please also provide scripts for the DDL and some testdata.

Why is my output parameter in stored procedure called from Entity Null?

I am calling a stored procedure and declare an output paramters:
CREATE PROCEDURE dbo.usp_getIsReadyForProcess
#VideoId INT ,
#val INT OUTPUT
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION
-- LOCK ROW UNTIL END OF TRANSACTION
SELECT * FROM dbo.ProcessStatus WITH (ROWLOCK, HOLDLOCK) WHERE VideoId = #VideoId
And then setting the value throughout the transaction
--If there is no row count
IF ##ROWCOUNT = 0
BEGIN
SET #val = 0
END
-- If video is already in process
ELSE IF #statusCode > 1
BEGIN
SET #val = 0
END
...... more if blocks
-- RELEASE LOCK
COMMIT TRANSACTION
END TRY
Here is the c# code for getting the output parameter:
using (var db = EntityFactory.GetInstance())
{
ObjectParameter objParam = new ObjectParameter("val", typeof(int));
db.usp_getIsReadyForProcess(videoId, objParam);
return (int)objParam.Value == 1;
}
... But then objParam.Value is null no matter what I do.
So I dig a little deeper and uncover an exception that was handled already,
Message:
Method may only be called on a Type for which Type.IsGenericParameter is true.
What am i doing wrong? I tried type(bool, int32, string) .. nothing works
Your stored procedure returns a resultset, due to this line:
SELECT * FROM dbo.ProcessStatus WITH (ROWLOCK, HOLDLOCK) WHERE VideoId = #VideoId
You can either change the SQL to something like:
DECLARE #RowCount INT
SELECT #RowCount = COUNT(*) FROM dbo.ProcessStatus WITH (ROWLOCK, HOLDLOCK) WHERE VideoId = #VideoId
Or you can capture the resultset in your code:
var resultSet = db.usp_getIsReadyForProcess(videoId, objParam);

Error in T-SQL trigger being ignored in .NET application

I've got a TableAdapter and I'm calling the Update(DataSet dataset) function of the adapter. There is a trigger on the underlying table that is throwing an error but this error is not causing an exception in the application after the select statement but it IS causing an exception if I just raise it at the beginning of the trigger. Any ideas?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[trigger1]
ON [dbo].[table1]
AFTER INSERT, UPDATE
AS
SET NOCOUNT ON;
SELECT a.description
FROM table1 a
INNER JOIN table2 b ON b.b_id = a.b_id
INNER JOIN inserted i ON i.b_id = a.b_id AND i.a_id = a.a_id
WHERE a.code = i.code
AND b.b_id <> i.b_id
AND a.description <> i.description
AND b.code IN (SELECT code FROM b WHERE b_id = i.b_id)
IF (##ROWCOUNT > 0)
BEGIN
RAISERROR ('ERROR', 16, 1)
ROLLBACK TRAN
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
It appears that you are examining ##ROW_COUNT to test for the existence of conflicting rows. Perhaps an EXISTS query would meet your needs:
IF EXISTS (
SELECT a.description
FROM table1 a
INNER JOIN table2 b ON b.b_id = a.b_id
INNER JOIN inserted i ON i.b_id = a.b_id AND i.a_id = a.a_id
WHERE a.code = i.code
AND b.b_id <> i.b_id
AND a.description <> i.description
AND b.code IN (SELECT code FROM b WHERE b_id = i.b_id)
)
BEGIN
RAISERROR ('ERROR', 16, 1)
ROLLBACK TRAN
RETURN
END
If the error still is not raised, then the business logic deserves a closer look.

Retrieve multiple data from stored procedure using Linq-to-SQL

I am using Silverlight and Linq-to-SQL to communicate with the database.
I have a stored procedure which receives 2 parameters (PFOID and Quantity) and Userid and returns a product name.
If we send multiple values like multiple pfoid's and quantity's it will return multiple product names shown as below
The stored procedure looks like this..
ALTER PROCEDURE [PFO].[PFOValidateUpdateData]
#PfoIDs xml, -- list of PFO ID's
#UserID uniqueidentifier --The Identity of the User making the call.
AS
BEGIN
-- SET DEFAULT BEHAVIOR
SET NOCOUNT ON -- Performance: stops rows affected messages
SET DEADLOCK_PRIORITY LOW -- This SP to be the Deadlock victim
-- Initialise Lock-Timeout and Deadlock vars for Insert
DECLARE #iLockTimeoutRetries as int
DECLARE #iDeadLockRetries as int
DECLARE #dtLockTimeoutSleepInterval as datetime
DECLARE #dtDeadlockSleepInterval as datetime
DECLARE #iErrorNumber as int
SET #iLockTimeoutRetries = 0
SET #iDeadLockRetries = 0
SET #dtLockTimeoutSleepInterval = sCommon.fnLockTimeoutSleepInterval()
SET #dtDeadlockSleepInterval= sCommon.fnDeadlockSleepInterval()
SET #iErrorNumber = 0
-- procedure specific
DECLARE #idoc as int
DECLARE #IsBrightstarUser as bit
RETRY:
BEGIN TRY
--Create Temp table to store stores!
CREATE TABLE [#PFOList]
(
[PFOId] nvarchar(50),
[Quantity] INT
)
--Create Temp table to store User stores!
CREATE TABLE [#UserStoreList]
(
[StoreID_XRef] nvarchar(50)
)
print CONVERT(varchar(1000), #PfoIDs)
--Create Document
EXEC sp_xml_preparedocument #idoc OUTPUT, #PfoIDs
-- Append to new list of Store records
INSERT INTO [#PFOList] (
[PFOId],
[Quantity]
)
SELECT [PFOID],[Quantity]
FROM OPENXML (#idoc, 'ArrayOfString/string',2)
WITH( [PFOID] nvarchar(50),[Quantity] [INT]) Stores
--WHERE [PFOId] Is Not NULL
-- Clean UP
exec sp_xml_removedocument #iDoc
-- are we dealing with a brightstar user?
SET #IsBrightstarUser = CASE WHEN exists
(SELECT *
FROM dbo.aspnet_UsersInRoles AS uir inner join
dbo.aspnet_Roles AS roles ON uir.RoleId = roles.roleid
WHERE roles.rolename = 'Brightstar Employee' and uir.userid = #userid)
THEN 1 ELSE 0 END
--Get User Storelist
INSERT INTO [#UserStoreList] (
[StoreID_XRef]
)
SELECT s.StoreId_XRef
FROM PFO.UserStoreLink us(nolock)
INNER JOIN PFO.Store s(nolock)
ON us.StoreId=s.StoreId
where UserId=#UserID
--Select * from [#PFOList]
--SELECT #IsBrightstarUser AS ISBrightstaruser
--SELECT * from [#UserStoreList]
--If BrightstarCustomer Update all the Quantities.
IF #IsBrightstarUser=1
BEGIN
UPDATE
PFO.PFO
SET
IsBrightstarReviewComplete = 1
,[ModifyingUsersID] = #UserID
,[ModifiedDate] = getdate()
,[PlannedQty] = pfol.[Quantity]
,[BrightstarReviewedQty]=pfol.[Quantity]
FROM
PFO.PFO as pfo
INNER JOIN [#UserStoreList] as stores on pfo.StoreId_XRef=stores.StoreID_XRef
INNER JOIN [#PFOList] as pfol on pfo.PFOId = pfol.PFOId
WHERE #IsBrightstarUser = 1
END
ELSE BEGIN
--Update Non Contrained Orders
UPDATE
PFO.PFO
SET
[ModifyingUsersID] = #UserID
,[ModifiedDate] = getdate()
,[PlannedQty] = pfol.[Quantity]
FROM
PFO.PFO (nolock) as pfo
INNER JOIN [#UserStoreList] as stores on pfo.StoreId_XRef=stores.StoreID_XRef
INNER JOIN [#PFOList] as pfol on pfo.PFOId = pfol.PFOId
WHERE pfo.IsBrightstarReviewComplete=1 AND IsConstraint=0
--SELECT * from PFO.PFO (nolock) where PFOId='04676723-2afb-49ff-9fa1-0131cabb407c'
--Update Contrained Orders
--Get Existing quantities for the User
CREATE TABLE #ExistingProductQuantity
(
[PfoID] nvarchar(100)
,[Product] nvarchar(255)
,[PlannedQty] INT
,[BrightstarReviewedQty] INT
)
CREATE TABLE #CustProductQuantity
(
[Product] nvarchar(255)
,[IsUpdatable] BIT
)
INSERT INTO #ExistingProductQuantity
( [PfoID],[Product],[PlannedQty],[BrightstarReviewedQty])
SELECT PFOId,InventoryId,PlannedQty,BrightstarReviewedQty
FROM PFO.PFO as pfo
INNER JOIN [#UserStoreList] as stores on pfo.StoreId_XRef=stores.StoreID_XRef
WHERE pfo.IsBrightstarReviewComplete=1 AND IsConstraint=1
UPDATE
#ExistingProductQuantity
SET [PlannedQty]=pfol.[Quantity]
FROM #ExistingProductQuantity eoq
INNER JOIN [#PFOList] as pfol on eoq.PFOId = pfol.PFOId
INSERT INTO #CustProductQuantity
( [Product],[IsUpdatable] )
SELECT
[Product],
CASE WHEN SUM(PlannedQty)<=SUM(BrightstarReviewedQty) THEN 1 ELSE 0 END
FROM #ExistingProductQuantity
GROUP BY [Product]
--SELECT * from #ExistingProductQuantity
--SELECT * from #CustProductQuantity
--Update the products that can be updatable
UPDATE
PFO.PFO
SET
[ModifyingUsersID] = #UserID
,[ModifiedDate] = getdate()
,[PlannedQty] = pfol.[Quantity]
FROM
PFO.PFO as pfo
INNER JOIN #UserStoreList as stores on pfo.StoreId_XRef=stores.StoreID_XRef
INNER JOIN #PFOList as pfol on pfo.PFOId = pfol.PFOId
INNER JOIN #CustProductQuantity as pr on pr.Product=pfo.InventoryId
WHERE pfo.IsBrightstarReviewComplete=1 AND pr.IsUpdatable=1 AND IsConstraint=1
--Return the products that are not updatabele
select [Product]
FROM #CustProductQuantity
where [IsUpdatable]=0
END
END TRY
BEGIN CATCH
-- Get the ErrorNumber
Set #iErrorNumber = ERROR_NUMBER()
--Handle Deadlock situation (Deletes, Inserts & Updates)
IF #iErrorNumber = 1205
BEGIN
-- If we have not made enough attempts to break the lock
IF #iDeadLockRetries < sCommon.fnMaxDeadlockRetries()
BEGIN
-- Increment the Attempt count
SET #iDeadLockRetries = #iDeadLockRetries + 1
-- Pause to allow the deadlock contention to clear
WAITFOR DELAY #dtDeadlockSleepInterval
GOTO RETRY
END
END
-- Handle Lock Timeout situation (Deletes, Inserts & Updates)
IF #iErrorNumber = 1222
BEGIN
-- If we have not made enough attempts to break the Deadlock
IF #iLockTimeoutRetries < sCommon.fnMaxLockTimeoutRetries()
BEGIN
-- Increment the Attempt count
SET #iLockTimeoutRetries = #iLockTimeoutRetries + 1
-- Pause to allow the lock contention to clear
WAITFOR DELAY #dtLockTimeoutSleepInterval
GOTO RETRY
END
END
exec Common.RethrowError
END CATCH
END
The result is as follows..
Product
6435LVWK-360-CD819E3
NSCHI535C1097I360-4C
NSCHU485C1819I360-0C
Return Value
0
My Linq-to-SQL connection is like this
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="PFO.PFOValidateUpdateData")]
public int PFOValidateUpdateData([global::System.Data.Linq.Mapping.ParameterAttribute(Name = "PfoIDs", DbType = "Xml")] System.Xml.Linq.XElement pfoIDs, [global::System.Data.Linq.Mapping.ParameterAttribute(Name = "UserID", DbType = "UniqueIdentifier")] System.Nullable<System.Guid> userID)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), pfoIDs, userID);
return ((int)(result.ReturnValue));
}
I am trying to retrieve all the data from the stored procedure but the when I debugging it the return value is "o"..
I would be grateful to you if you could help me retrieve all the data returned by the stored procedure... thank you very much...
If your stored procedure returns a collection of nvarchar's, then the signature of your Linq2Sql method is not correct. It should not return an int, but an ISingleResult.
So the correct signature will be:
public ISingleResult<string> PFOValidateUpdateData(...)
{
IExecuteResult result = this....;
return (ISingleResult<string>)result.ReturnValue;
}
var products = PFOValidateUpdateData(...).ToList();
If you want to return the results from multiple SELECT's in your stored procedure, you'll have to use IMultipleResults.
Well I know this is not the right way...for time being,its working for me...
I created an other table with two columns one ProductId and ID, I am inserting the values returned by the stored procedure,
in the designer.cs I am returning the table,
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="PFO.PFOValidateUpdateData")]
public ISingleResult<PFOValidData> PFOValidateUpdateData([global::System.Data.Linq.Mapping.ParameterAttribute(Name = "PfoIDs", DbType = "Xml")] System.Xml.Linq.XElement pfoIDs, [global::System.Data.Linq.Mapping.ParameterAttribute(Name = "UserID", DbType = "UniqueIdentifier")] System.Nullable<System.Guid> userID)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), pfoIDs, userID);
return ((ISingleResult<PFOValidData>)(result.ReturnValue));
}
And in the Domainservice
List<string> PFOValidateUpdateData(string pfoIds, Guid userID)
{
List<string> productIdList = new List<string>();
// Acquire the int
result = this.DataContext.PFOValidateUpdateData(element, userID);
foreach (var item in result)
{
productIdList.Add(item.ProductID);
}
return productIdList;
To get the multiple values returned by the stored procedure....
Please let me know if there is a better way to solve this... thank you

Error in MySql Query Syntax

I am trying to create a MySql stored procedure through C#. I have created some other as well but when I try to execute the Query via MySqlCommand.ExecuteNonQuery its throws a exception that you query syntax is not compatible. Here is the exception message:
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'SET
VariableRecordExists = (SELECT
COUNT(*) FROM SmartCache_Sync WHERE
MachineNa' at line 10
And here I am trying to build the query string:
string sql = #"CREATE PROCEDURE SmartCache_UpdateSync
(
VariableMachineName varchar(50)
)
BEGIN
DECLARE VariableRecordExists int;
DECLARE VariableSetDate datetime;
START TRANSACTION;
SET VariableSetDate= Now()
SET VariableRecordExists = (SELECT COUNT(*) FROM SmartCache_Sync WHERE MachineName = VariableMachineName)
IF VariableRecordExists = 1
THEN
UPDATE SmartCache_Sync
SET LastUpdate = VariableSetDate
WHERE MachineName= VariableMachineName;
ELSE
INSERT INTO SmartCache_Sync
(MachineName,LastUpdate)
VALUES (VariableMachineName,VariableSetDate);
END IF;
COMMIT;
SELECT VariableSetDate;
END";
I don't know where I am making a mistake. Probably I am missing a semi colon ; somewhere or what. I would be obliged if anyone could help me.
I don't know about making queryies in c#, but normally the ; is the end of your query: so if you don't change the delimiter, you are ending your command early. normally you would do something like this:
delimiter //
CREATE PROCEDURE simpleproc (OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END//
delimiter ;
from:
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
Slightly off-topic - but...
In general, prefer "if exists (select...)" to "select count(*)..." when all you want to do is check if any rows exist. It is far cheaper than actually counting all the rows.
And secondly, it looks as though you are trying to do an "upsert", which in MySQL would be
INSERT INTO SmartCache_Sync(MachineName,LastUpdate)
VALUES (VariableMachineName,VariableSetDate)
ON DUPLICATE KEY UPDATE LastUpdate = VariableSetDate
and then you don't need the explicit transaction either.
This of course assumes that MachineName is a primary key, which I'm guessing it is.
My guess was right I was missing a ";". And honestly speaking it took me 2 hours to that out.
**
SET VariableSetDate= Now();
SET VariableRecordExists = (SELECT COUNT(*) FROM SmartCache_Sync
WHERE MachineName =
VariableMachineName);
**
Both the statements didn't have ";" at the end
CREATE PROCEDURE SmartCache_UpdateSync
(
VariableMachineName varchar(50)
)
BEGIN
DECLARE VariableRecordExists int;
DECLARE VariableSetDate datetime;
START TRANSACTION;
SET VariableSetDate= Now()
(SELECT VariableRecordExists = COUNT(*) FROM SmartCache_Sync WHERE MachineName = VariableMachineName)
IF VariableRecordExists = 1
THEN
UPDATE SmartCache_Sync
SET LastUpdate = VariableSetDate
WHERE MachineName= VariableMachineName;
ELSE
INSERT INTO SmartCache_Sync
(MachineName,LastUpdate)
VALUES (VariableMachineName,VariableSetDate);
END IF;
COMMIT;
SELECT VariableSetDate;
END";

Categories