For each item (actually a value, "ut" in the code) selected from the table #pub, I want to create some tables based on this "ut" value and then, if some criteria are satisfied, I will print this "ut". But there are some errors in my SQL Server code shown below.
select ut
into #pub
from wosclassification1813..clustering
where cluster_id1 = 145
DECLARE #temp_ut NVARCHAR(MAX);
DECLARE #citation_count INT;
DECLARE #dccp_count INT;
DECLARE #temp_string_1 NVARCHAR(MAX);
DECLARE #temp_string_2 NVARCHAR(MAX);
DECLARE #i INT;
SET #i = 1;
DECLARE #j INT;
SET #j = 0;
DECLARE #n INT;
SET #n = (select count(*) from #pub);
Declare Cur Cursor For select ut from #pub
Open Cur
Fetch next From Cur Into #temp_ut
WHILE #j < #n
Begin
SET #temp_string_1 = (CONVERT(NVARCHAR, #i))
EXEC('select s_ut as ut into #temp_string_1 from woskb.dbo.cwts_pairs where c_ut = #temp_ut')
SET #temp_string_2 = (CONVERT(NVARCHAR, #i))
EXEC('select a.c_ut as c_ut, a.s_ut as s_ut into temp_string_2 from woskb.dbo.cwts_pairs as a join #temp_string_1 as b on a.c_ut = b.ut join #temp_string_1 as c on a.s_ut = c.ut')
SET #dccp_count = (select count(*) from #dccp)
SET #citation_count = (select count(*) from #citing_pub)
SET #i = #i + 1
SET #j = #j + 1
if #dccp_count = #citation_count*(#citation_count-1)/2
print #temp_ut
Fetch Next From Cur Into #temp_ut
End
Close Cur
Deallocate Cur
Related
I am trying to execute an Update statement on a table. This statement is placed within Cursor and While block. I have checked in debugger and the values are coming into the statements and variable, still the update is not putting values into the table fields. Please advise what am I doing wrong here.
ALTER PROCEDURE SP_PO1 #P1 int
AS
BEGIN
SET NOCOUNT ON;
DECLARE #DOC AS INT;
DECLARE #CASH AS FLOAT;
DECLARE #TENTYPE AS VARCHAR(100);
DECLARE #UDF AS VARCHAR(100);
DECLARE #COUNTER AS INT;
DECLARE #SQL AS VARCHAR(500);
SELECT #DOC=DOCTYPE FROM InvNum WHERE AutoIndex = #P1;
IF #DOC = 6
BEGIN
SET #COUNTER = 1;
DECLARE Cur_Tender CURSOR FOR
SELECT Tender.TenderNo FROM Tender;
OPEN CUR_TENDER;
FETCH NEXT FROM CUR_TENDER INTO #TENTYPE;
WHILE ##FETCH_STATUS = 0
BEGIN
SELECT #CASH = ISNULL(_btblPOSTenderTx.fTxAmount,0) FROM _btblPOSTenderTx INNER JOIN Tender ON _btblPOSTenderTx.iTenderID = Tender.IdTender INNER JOIN _btblPOSXZTable ON _btblPOSTenderTx.iPOSXZTableID = _btblPOSXZTable.IDPOSXZTable WHERE (_btblPOSXZTable.iTillTxType = 7) and (_btblPOSXZTable.IDPOSXZTable = (select Max(IDPOSXZTable) from [dbo].[_btblPOSXZTable])) AND (TenderNo = #TENTYPE);
SET #UDF = 'ufIDPOSInvTENDER' + CONVERT(VARCHAR(2),#COUNTER);
UPDATE InvNum SET #UDF=#CASH WHERE AutoIndex = #P1;
SET #COUNTER = #COUNTER + 1;
FETCH NEXT FROM CUR_TENDER INTO #TENTYPE;
END
END
CLOSE CUR_TENDER
DEALLOCATE CUR_TENDER
END
GO
You update variable
UPDATE InvNum SET #UDF=#CASH WHERE AutoIndex = #P1;
update table column
UPDATE InvNum SET <column>=#CASH WHERE AutoIndex = #P1;
if you want a dynamic column name - use dynamic sql
EXEC('UPDATE InvNum SET ' + #UDF + '=' + CAST(#CASH as VARCHAR(50) + ' WHERE AutoIndex = ' + CAST(#P1 as VARCHAR(5) ' );
I have 64 columns and I am trying to automate the loop process. The loop runs but it shows 0 affected rows. If I update the table, column by column, it works.
Any idea why its showing 0 affected rows and what can be done ?
update temp set col1 = 'C' where col1 IS Null; -- works (276 rows affected)--
declare #count as int;
declare #name as varchar(max);
set #count = 2;
while #count < (SELECT Count(*) FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'temp')+1
Begin
Set #name = (select name from (select colorder, name from (SELECT *
FROM syscolumns WHERE id=OBJECT_ID('temp')) colnames) as cl where colorder = #count)
Print #name
update temp set #name = 'C' where #name IS Null;
SET #count = #count + 1;
END;
You need to use dynamic sql to update the different columns during runtime as below.
Note: I just added/modified the dynamic sql part.
declare #count as int;
declare #name as varchar(max)
declare #sql nvarchar (1000)
set #count = 2
while #count < (SELECT Count(*) FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'temp')+1
Begin
Set #name = (select name from (select colorder, name from (SELECT *
FROM syscolumns WHERE id=OBJECT_ID('temp')) colnames) as cl where colorder = #count)
Print #name
set #sql = N'update temp set ' + #name + '= ''C'' where ' + #name + ' is null ';
exec sp_executesql #sql
SET #count = #count + 1
END;
Just a quick question, how can i optimize following my SQL server query and also, any way to replace while loop while loop?...Thanks
DECLARE #TOTAL INT, #COUNT INT=1, #ID INT,#FROMDATE DATE= '2016-11-01', #TODATE= '2016-11-10'
SELECT #TOTAL = COUNT(1)
FROM NUMBERS
WHILE #COUNT <= #TOTAL
BEGIN
SELECT #ID = ID
FROM #TEMP
WHERE ROW_ID = #COUNT
DECLARE #OUTPUT VARCHAR(MAX) = '';
EXEC SPSAMPLEPROC #ID, #FROMDATE, #TODATE, #OUTPUT OUTPUT;
SELECT #COUNT = #COUNT + 1;
END
I´m having some trouble about this query. The problem is that only one while is process, the firts one, don´t work for me.
This is my code:
The While #j, don´t work, but I don´t see the mistake ...
Thanks for your time.
CREATE PROCEDURE InsertVar
AS
DECLARE #i int
DECLARE #j int
SET NOCOUNT ON;
SET #i = 1
SET #j = 1
WHILE #j < 21
BEGIN
WHILE #i < 11
BEGIN
INSERT INTO Var(idline,idVar,CheckBox )
VALUES(#j,#i,0);
SET #i = #i + 1;
END;
SET #j = #j + 1;
END;
SELECT * FROM Var;
GO
There are other ways to do this logic (such as a single query). But your problem is that you do not re-initialize i inside the loop. Try this:
CREATE PROCEDURE InsertVar
AS
BEGIN
DECLARE #i int;
DECLARE #j int;
SET NOCOUNT ON;
SET #j = 1;
WHILE #j < 21
BEGIN
SET #i = 1;
WHILE #i < 11
BEGIN
INSERT INTO Var(idline, idVar, CheckBox)
VALUES(#j, #i, 0);
SET #i = #i + 1;
END;
SET #j = #j + 1;
END;
SELECT * FROM Var;
END;
GO
Use Recursive CTE to generate the rows. No need to use while loop or declaration of varaiables.
CREATE PROCEDURE Insertvar
AS
BEGIN
SET NOCOUNT ON;
;WITH cte
AS (SELECT 1 AS idline
UNION ALL
SELECT idline + 1 FROM cte
WHERE idline < 20),
cte1
AS (SELECT idline,1 AS idVar,0 AS CheckBox
FROM cte
UNION ALL
SELECT idline,idVar + 1,CheckBox FROM cte1
WHERE idVar < 10)
INSERT INTO [Var]
SELECT * FROM cte1
ORDER BY idline,idVar
SELECT * FROM [Var]
END
This is the stored procedure I'm using to get 5 digits from an xml:
CREATE PROCEDURE [dbo].[SP_KINGPRICE_InsertJournalFromPost]
(
#ResponseID bigint,
#TransactionDetailID bigint
)
AS
BEGIN
DECLARE #info as varchar(max) = '', #Reference as varchar(max) = ''
SET #info = (SELECT SUBSTRING(Response, CHARINDEX('<GlJournal>',Response) + 11,5)
FROM SysproIntegration..ifmTransactionDetailResponse
WHERE TransactionDetailResponseID = #ResponseID)
SET #Reference = (SELECT DISTINCT Reference
FROM GenJournalDetail
WHERE Journal = CAST(#info as DECIMAL(5,0)))
INSERT INTO ZJournalRecords
(JournalNumber,Reference)
VALUES (#info,#Reference)
END
The XML has a tag like this:
<GLJournal>12345</GLJournal>
If the XML document has only one of these tags, I have no worries. The SP works fine. The problem comes in when there are two nested <GLJournal> tags. The xml would then look something like:
<GLJournal>
<SomeTag/>
<SomeTag2/>
<GLJournal>12345</GLJournal>
</GLJournal>
How can I get the 5 digit value of the nested tag? (It will always be 5 digits)
I have thought of using a try catch, but that doesn't seem like an elegant solution.
EDIT:
Also, part of the problem is, I don't know when there will be one GlJournal tags, or two.
I decided to count the number of occurrences of the tag, and based on that, substring a certain position.
DECLARE #info as varchar(max) = '', #Reference as varchar(max) = '', #inputString as varchar(max), #searchString as varchar(max), #numberOfTags as int
SET #searchString = '<GlJournal>'
SET #inputString = (SELECT Response FROM SysproIntegration..ifmTransactionDetailResponse WHERE TransactionDetailResponseID = #ResponseID)
SET #numberOfTags = (LEN(#inputString) -
LEN(REPLACE(#inputString, #searchString, ''))) /
LEN(#searchString)
IF #numberOfTags = 1
BEGIN
SET #info = (SELECT SUBSTRING(Response, CHARINDEX('<GlJournal>',Response) + 11,5) FROM SysproIntegration..ifmTransactionDetailResponse WHERE TransactionDetailResponseID = #ResponseID)
SET #Reference = (SELECT DISTINCT Reference FROM GenJournalDetail WHERE Journal = CAST(#info as DECIMAL(5,0)))
END
ELSE
BEGIN
SET #info = (SELECT SUBSTRING(Response, CHARINDEX('<GlJournal>',Response) + 69,5) FROM SysproIntegration..ifmTransactionDetailResponse WHERE TransactionDetailResponseID = #ResponseID)
SET #Reference = (SELECT DISTINCT Reference FROM GenJournalDetail WHERE Journal = CAST(#info as DECIMAL(5,0)))
END
Here's an int test you might adapt:
declare #xml xml, #tags smallint
SET #tags = 1
-- Load #xml...
--SELECT #xml = Response
--FROM SysproIntegration..ifmTransactionDetailResponse
--WHERE TransactionDetailResponseID = #ResponseID
-- ... or for test...
IF #tags = 1 BEGIN
-- Test 1 GLJournal tag
SET #xml = '12346'
END ELSE IF #tags = 2 BEGIN
-- Test 2 GLJournal tags
SET #xml = '
12345
'
END ELSE BEGIN
-- Test no GLJournal tags
SET #xml = ''
END
DECLARE #i int; SET #i = -1
-- Try one tag
SELECT #i = ParamValues.ID.query('GLJournal').value('.','int')
FROM #xml.nodes('/') as ParamValues(ID)
IF #i = 0
-- Try two tags
SELECT #i = ParamValues.ID.query('GLJournal').value('.','int')
FROM #xml.nodes('/GLJournal') as ParamValues(ID)
SELECT #i
-- INSERT INTO ZJournalRecords... other stuff