How to insert select to stored procedure with insert? - sql

I try to create stored procedure, but I had syntax error and I don't know what to change. I want to receive number from the select query to the "#cartypeID" for insert the car type number the the insert code below.
create PROCEDURE AddCar (#CarFleetID int,#CarModelName nvarchar(50),#CarNumber nvarchar(50),#CurrentMiles int,#ImageFileName nvarchar(50),#IsOk2Rent bit)
AS
IF (SELECT COUNT(1) FROM CarFleet WHERE CarFleetID =#CarFleetID ) = 1
BEGIN
RETURN -1
END ELSE
BEGIN
declare #cartypeID int
#cartypeID= select CarFleet.CarFleetID
from CarFleet join CarTypes on CarFleet.CarTypeID=CarTypes.CarTypeID join CarModels on CarTypes.CarModelID=CarModels.CarModelID
where #CarModelName=CarModels.CarModelName
INSERT INTO CarFleet(CarFleetID,CarTypeID,CarNumber,CurrentMiles,ImageFileName,IsOk2Rent)
VALUES(#CarFleetID, #cartypeID, #CarNumber,#CurrentMiles,#ImageFileName,#IsOk2Rent)
END

I think
create PROCEDURE AddCar (#CarFleetID int,#CarModelName nvarchar(50),#CarNumber nvarchar(50),#CurrentMiles int,#ImageFileName nvarchar(50),#IsOk2Rent bit)
AS
IF (SELECT COUNT(1) FROM CarFleet WHERE CarFleetID =#CarFleetID ) = 1
BEGIN
RETURN -1
END ELSE
BEGIN
INSERT INTO CarFleet (
CarFleetID
,CarTypeID
,CarNumber
,CurrentMiles
,ImageFileName,IsOk2Rent)
select
CarFleet.CarFleetID
,#cartypeID
,#CarNumber
,#CurrentMiles
,#ImageFileName,#IsOk2Rent
from CarFleet
join CarTypes on CarFleet.CarTypeID=CarTypes.CarTypeID
join CarModels on CarTypes.CarModelID=CarModels.CarModelID
where #CarModelName=CarModels.CarModelName
END

Related

SQL IF inside a Function

ALTER FUNCTION [dbo].[FSmapping] (
#runID as bigInt,
#TypMap as char(1)
)
RETURNS TABLE
AS
RETURN
IF (#TypMap ='C')
BEGIN
SELECT b.[A/C],g.accountNumber, b.[Process], g.[endingBalanceLC], g.beginningBalanceLC
FROM table1 g
LEFT JOIN table2 b ON b.[Acc2]=g.Account_Synt
where RunID = #runID
END
I am trying to implement a table function in SQL. Basically it should have 3 more IFs and the only thing that's going to switch is the table2, from table2-4 based on inputs C I R S. The error is the IF statement, I know that something is missing, I am having a hard time implementing some sort of a switch function with table as a return value. The Select works flawlessly without the IF syntax, it also says errors on the two variables inside the IF
Must declare the scalar variable runID and TypMap
You need to change your query as multi-statement table-valued functions so your function has to like as below:
CREATE OR ALTER FUNCTION [dbo].[FSmapping] (
#runID as bigInt,
#TypMap as char(1)
)
RETURNS #Table1JoinTable2
TABLE (
--table definition here
Col1 VARCHAR(10)
)
AS
BEGIN
IF (#TypMap ='C')
BEGIN
INSERT INTO #Table1JoinTable2
SELECT #TypMap
--Put select query SELECT b.[A/C],g.accountNumber, b.[Process], g.[endingBalanceLC], g.beginningBalanceLC
--FROM table1 g
--LEFT JOIN table2 b ON b.[Acc2]=g.Account_Synt
--where RunID = #runID
END
IF (#TypMap ='I')
BEGIN
INSERT INTO #Table1JoinTable2
SELECT #TypMap
--Put your select query
END
IF (#TypMap ='I')
BEGIN
INSERT INTO #Table1JoinTable2
SELECT #TypMap
--Put your select query
END
IF (#TypMap ='R')
BEGIN
INSERT INTO #Table1JoinTable2
SELECT #TypMap
--Put your select query
END
IF (#TypMap ='S')
BEGIN
INSERT INTO #Table1JoinTable2
SELECT #TypMap
--Put your select query
END
RETURN
END
If TypMap does not come C, what will the function return?
i think u need 'ELSE'.
IF Boolean_expression
BEGIN
-- Statement block executes when the Boolean expression is TRUE
END
ELSE
BEGIN
-- Statement block executes when the Boolean expression is FALSE
END

While Loop SAP B1 SQL stored procedure for blocking

I have an issue with my stored procedure SAP B1.
What I'm trying to do here is storing the sum of the quantity, group by the manufacturing order and insert it into the temp table. Then use a while loop to go thru each ID to compare with the user table [#FGTRACKING] and block if
temp.quantity > sum(quantity) in [#FGTracking].
However this is not working, the transaction still passed the stored procedure block. I suspect there is something wrong with my syntax.
IF #transaction_type IN ('A') AND #object_type = '67'
BEGIN
declare #top as int
declare #temp table (id int,quantity int NOT NULL, monum int NOT NULL)
insert into #temp (id, quantity,monum)
select row_number() over (order by (select NULL)), sum(quantity) as quantity, u_shipment_line as monum
from wtr1 t1
where t1.docentry = #list_of_cols_val_tab_del
group by u_shipment_line
set #top = 1
WHILE #top <= (select count(monum) from #temp)
BEGIN
IF EXISTS (select t100.monum from #temp t100
where t100.quantity > (select sum(t111.u_transfer)
from [#FGTRACKING] t111 where t111.u_mo_num = t100.monum
group by t111.u_mo_num) and t100.id = #top)
BEGIN
SELECT #Error = 666, #error_message = 'Over-transfer'
END
ELSE
set #top = #top + 1
END
END
It looks like you're only incrementing your iterator (#top) when you don't encounter your error condition, so if your error condition triggers, you're stuck in an infinite loop.
Get rid of your "else" and always increment #top, or alternatively break out of your while loop when you hit your error condition.
...
ELSE -- Get rid of this else
set #top = #top + 1
...

Insert Query to insert multiple rows in a table via select and output clause. SQL Server 2008

I have a created a stored procedure (please ignore syntax errors)
alter proc usp_newServerDetails
(#appid int, #envid int, #serType varchar(20), #servName varchar(20))
as
declare #oTbl_sd table (ID int)
declare #outID1
declare #oTbl_cd table (ID int)
declare #outID2
begin Transaction
insert into server_details(envid, servertype, servername)
output inserted.serverid into #oTbl_sd(ID)
values(#envid, #serType, #servName)
select #outID1 = ID from #oTbl_sd
insert into configdetails(serverid, servertype, configpath, configtype)
output inserted.configid into #oTbl_cd(ID)
(select #outID1, cm.servertype, cm.configpath, cm.configtype
from configpthmaster cm
where cm.appid = #appid )
select #outID2 = ID from #oTbl_cd
insert into configkeydetails(confiid, keyname)
output inserted.Keyid into #oTbl_ckd(ID)
(select #outID2, cm.key
from configpthmaster cm
where cm.appid = #appid)
begin
commit
end
server_details table has an identity column ID with is auto-generated ie. #outID1 and first insert query inserts only 1 row.
configpthmaster table is not related to any other table directly and has 2 unique data rows, which I want to fetch to insert data into other tables, one by one during insertion.
The second insert query fetch data from configpthmaster table
and insert 2 rows in configdetails while generating (auto-generated) ID ie. #outID2.
It also has a FK mapped to server_details.
The problem is "#outID2" giving last inserted ID only (ie. if two id generated 100,101 i am getting 101) which eventually on 3rd insertion, inserting 2 rows with same id 101 only but i want the insertion should be linear. i.e one for 100 and other for 101.
If zero rows affected while insertion how to rollback the transaction?
How can I achieve these requirements? Please help.
Change your procedure like below,and try again.
ALTER PROCEDURE usp_newServerDetails(#appid int, #envid int,#serType varchar(20),#servName varchar(20))
AS
BEGIN
BEGIN TRY
DECLARE #Output TABLE (ID int,TableName VARCHAR(50),cmKey VARCHAR(50)) --table variable for keeping Inserted ID's
BEGIN TRAN
IF EXISTS ( SELECT 1 FROM configpthmaster cm WHERE cm.appid = #appid )
AND ( SELECT 1 FROM configkeydetails ck WHERE ck.appid = #appid ) --add a conditon to satisfy the valid insertions
BEGIN
INSERT INTO server_detials(envid,servertype,servername)
OUTPUT inserted.serverid,'server_detials',NULL INTO #Output(ID,TableName,cmKey )
VALUES(#envid ,#serType ,#servName)
INSERT INTO configdetails(serverid,servertype,configpath,configtype)
OUTPUT inserted.configid,'configdetails',cm.Key INTO #Output(ID,TableName,cmKey )
SELECT t.ID,cm.servertype,cm.configpath,cm.configtype
FROM configpthmaster cm
CROSS APPLY (SELECT ID FROM #Output WHERE TableName='server_detials')t
WHERE cm.appid = #appid
INSERT INTO configkeydetails(configId,keyname)
SELECT ID,cmKey FROM #Output
WHERE TableName='configdetails'
END
COMMIT TRAN
END TRY
BEGIN CATCH
IF ##TRANCOUNT > 0
ROLLBACK
END CATCH
END
Could you try this solution?
alter proc usp_newServerDetails(#appid int, #envid int,#serType varchar(20),#servName varchar(20))
as
declare #oTbl_sd table (ID int)
declare #outID1
declare #oTbl_cd table (ID int)
declare #outID2
begin Transaction
insert into server_detials(envid,servertype,servername)
output inserted.serverid into #oTbl_sd(ID)
values(#envid ,#serType ,#servName)
select #outID1 = ID from #oTbl_sd
insert into configdetails(serverid,servertype,configpath,configtype)
output inserted.configid into #oTbl_cd(ID)
(select #outID1 ,cm.servertype,cm.configpath,cm.configtype from configpthmaster cm where cm.appid = #appid )
select #outID2 = ID from #oTbl_cd
insert into configkeydetails(confiid,keyname)
output inserted.Keyid into #oTbl_ckd(ID)
(select isnull(replace(stuff((SELECT inserted.configid FOR xml path('')), 1, 1, ''), '&', '&'), '') ,cm.key, from configpthmaster cm where cm.appid = #appid )
begin
commit
end
I just added STUFF in your code.
The STUFF function inserts a string into another string.
Do take note that using STUFF drastically slows the processing time of the code.
for more information about STUFF

get error in sql stored procedure table type

i have this procedure and i want insert list of value into table,and i want check duplicate value and return this but i get this error
ERROR:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
ALTER PROCEDURE [dbo].[insertData]
#Value insertTbl READONLY,
#Result INT OUTPUT
AS
BEGIN
BEGIN TRY
IF (SELECT COUNT(Id)
FROM #Values
WHERE Id IN (SELECT Id
FROM [dbo].[username])) = 0
BEGIN
INSERT INTO dbo.username ( Id, NAME )
SELECT Id,
NAME
FROM #Values
WHERE Id NOT IN (SELECT Id
FROM [dbo].[username])
SELECT #Result = 101
END
ELSE
SELECT #Result = (
SELECT Id
FROM #Values
WHERE Id IN (SELECT Id
FROM [dbo].[username])
)
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
END CATCH
END
Please try this.
I have changed the return type to VARCHAR. That would return a CSV (e.g. 1,3,9...)
The other option is to return a result set. For this replace SELECT #Result = to INSERT INTO #Result...
Hope this helps.
ALTER PROCEDURE [dbo].[insertData]
#Value insertTbl READONLY,
#Result VARCHAR(500) OUTPUT
AS
BEGIN
BEGIN TRY
IF (SELECT COUNT(Id)
FROM #Values
WHERE Id IN (SELECT Id
FROM [dbo].[username])
) = 0
BEGIN
INSERT INTO dbo.username (Id,NAME)
SELECT Id,
NAME
FROM #Values
WHERE Id NOT IN (SELECT Id
FROM [dbo].[username])
SELECT #Result = 101
END
ELSE
BEGIN
SELECT #Result = (SELECT STUFF((SELECT ', ' + CAST(ID AS VARCHAR(5))
FROM #Values
WHERE Id IN (SELECT Id
FROM [dbo].[username])
ORDER BY Id FOR XML PATH('')),1,2,''))
END
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
END CATCH
END
Probably this line
SELECT #Result=(SELECT Id FROM #Values WHERE Id IN (SELECT Id FROM [dbo].[username]))
return more than one row. You should change the query to return only one value as below
SELECT #Result=(SELECT top 1 Id FROM #Values WHERE Id IN (SELECT Id FROM [dbo].[username]))
or you should change your logic. Alternative way is to use temprary table to return listo of ID. Try below solution
-- create temporary table before (!) creating procedure
create table #Resulttab
(
Result int
)
go
ALTER PROCEDURE [dbo].[insertData]
#Value insertTbl READONLY,
#Result INT OUTPUT -- in this solution I think you don't need this parameter
AS
BEGIN
BEGIN TRY
IF (SELECT COUNT(Id)
FROM #Values
WHERE Id IN (SELECT Id
FROM [dbo].[username])) = 0
BEGIN
INSERT INTO dbo.username ( Id, NAME )
SELECT Id,
NAME
FROM #Values
WHERE Id NOT IN (SELECT Id
FROM [dbo].[username])
SELECT #Result = 101
END
ELSE
insert into #Resulttab
SELECT Id
FROM #Values
WHERE Id IN (SELECT Id
FROM [dbo].[username])
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
END CATCH
END
go
You can use it that way:
-- create temporary table before run procedure
create table #Resulttab
(
Result int
)
-- run procedure with parameters
exec insertData ...
--after run procedure you can check list of your IDs
select Result from #Resulttab

How do i use rowcount in Function in sqlserver?

I have faced a problem using rowcount in function in sql server 2000.
It shows an error like Invalid use of 'UNKNOWN TOKEN' within a function.
MY function is like this.
ALTER Function fnc_GetOpenShiftWorkID (#EMP_ID int,#Counter int,#date Datetime) returns int as
BEGIN
SET ROWCOUNT #Counter
declare #result int
if exists(select * from tbl_org_workinghrs WHERE EMP_ID=#EMP_ID and SDATE=#DATE)
BEGIN
select #result= WORK_ID
from tbl_org_working_hrs work_hrs
inner join tbl_org_shift_group sgroup on sgroup.WH_ID=work_hrs.WORK_ID
inner join tbl_org_workinghrs workhrs on workhrs.GROUP_ID=sgroup.GROUP_ID
WHERE EMP_ID=#EMP_ID
and SDATE=#DATE
order by
IN_START
END
ELSE
BEGIN
if exists(select * from tbl_org_workinghrs where EMP_ID=0)
BEGIN
select #result=WORK_ID
from tbl_org_working_hrs
WHERE IS_DEFAULTSHIFT=1
END
END
return #result
END
You want to get the value of the n'th row ordered by IN_START.
From SQL Server 2005 later you could use top(n) or row_number().
In SQL Server 2000 you can use a table variable with an identity ID field as a temp storage.
Something like this.
declare #T table
(
ID int identity,
WORK_ID int
)
insert into #T (WORK_ID)
select WORK_ID
from tbl_org_working_hrs work_hrs
inner join tbl_org_shift_group sgroup
on sgroup.WH_ID=work_hrs.WORK_ID
inner join tbl_org_workinghrs workhrs
on workhrs.GROUP_ID=sgroup.GROUP_ID
where EMP_ID=#EMP_ID and
SDATE=#DATE
order by IN_START
select #result = WORK_ID
from #T
where ID = #Counter