Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm getting the following error for this simple piece of SQL code. Any ideas why? Any BEGIN ... END block causes this error to occur
/* SQL Error (156): Incorrect syntax near the keyword 'END'. */
DECLARE #PROCESSEDCOUNT INT = 0
WHILE (#PROCESSEDCOUNT < 10)
BEGIN
SET #PROCESSEDCOUNT +=1
IF 1=1
BEGIN
-- Stuff happens here
END
END
This is running on SQL Server - MSSQL15
You must put something in between the BEGIN and END block
For example:
DECLARE #PROCESSEDCOUNT INT= 1
WHILE (#PROCESSEDCOUNT < 10)
BEGIN
SET #PROCESSEDCOUNT +=1
IF 1=1
BEGIN
PRINT '1'
END
END
You need more than a comment in a begin/end block.
Try the following
IF 1=1
BEGIN
SELECT #PROCESSEDCOUNT
END
You don't have any condition for the inner BEGIN...END. You can remove it.
DECLARE #PROCESSEDCOUNT INT = 0
WHILE (#PROCESSEDCOUNT < 10)
BEGIN
SET #PROCESSEDCOUNT +=1
-- Stuff happens here
END
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
My number needs to be 8 digits long, however If its less than 8 digits long I need to add trailing zeros to the it.
Example: 1234
Desired result: 12340000
I tried this at first:
DECLARE #YourNumber VARCHAR(8)=1234567;
SELECT DISTINCT
LEFT('00000000'+CAST(ISNULL(#YourNumber,0) AS VARCHAR),8)
However the result is: 00000000
I have the same read as #Hogan +1. I just tend to opt for concat(). No need to test for nulls or even care if the value is a string or int
Example
Select IfInt = left(concat(1234 ,'00000000'),8)
,IfStr = left(concat('1234','00000000'),8)
,IfNull= left(concat(null ,'00000000'),8)
Results
IfInt IfStr IfNull
12340000 12340000 00000000
If what you are asking for is actually what you want then try this:
DECLARE #YourNumber VARCHAR(8)='1234567';
SELECT DISTINCT
LEFT(CAST(ISNULL(#YourNumber,0) AS VARCHAR)+'00000000',8)
Since you are starting with a "number" in a string (DECLARE #YourNumber VARCHAR(8)=1234567;) there is no need to use cast. You can simply add the required number of zeroes:
DECLARE #YourNumber VARCHAR(8)= '1234567'; -- Using a string rather than an Int literal.
select #YourNumber + Replicate( '0', 8 - Len( #YourNumber ) ) as PaddedString;
Aside: It is a best practice to always specify the length of strings, i.e. CAST(ISNULL(#YourNumber,0) AS VARCHAR(8)).
All you have to do is move the "+ '00000000'" portion of the code to the right side of the Cast Function.
DECLARE #YourNumber VARCHAR(8)=1234567;
SELECT DISTINCT
LEFT(CAST(ISNULL(#YourNumber,0) AS VARCHAR)+'00000000',8)
Resulting in final value of: 12345670
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
if [ "${ARRAY_SIZE}" = "" ];then
ARRAY_SIZE=10000
fi
declare
type t_reqseqArray is table of TEMP_TABLE_BKP.REQ_SEQ%type;
v_reqseqArray t_reqseqArray;
v_array_limit number(5) := ${ARRAY_SIZE};
v_commit_rate number(5) := ${COMMIT_RATE};
v_fetch_count number(5) := 0;
cursor c_reqseq is select req_seq from TEMP_TABLE_bkp;
v_TEMP_TABLE_bkp number(16) := 0;
v_array number(16) := 0;
begin
open c_reqseq;
loop
v_fetch_count := v_fetch_count + 1;
fetch c_reqseq bulk collect into v_reqseqArray limit v_array_limit;
v_array := v_reqseqArray.count;
dbms_output.put_line('Array count, '||to_char(v_array,'9999999')||' Request Sequences got');
forall idx in 1..v_reqseqArray.count
update TEMP_TABLE_bkp set req_seq=req_seq+1 where req_seq = v_reqseqArray(idx);
v_TEMP_TABLE_bkp := sql%rowcount;
exit when c_reqseq%notfound;
if (v_fetch_count >= v_commit_rate) then
dbms_output.put_line('Commit point reached, '||to_char(v_TEMP_TABLE_bkp,'9999999')||' Request Sequences got updated.');
${COMMIT};
v_fetch_count := 0;
end if;
end loop;
close c_reqseq;
end;
/
And result shows, Array count as 10000 where as SQL row count showed 12907.
Array count, 10000 Request Sequences got
12907
Commit point reached, 12907 Request Sequences got updated.
Array count, 10000 Request Sequences got
8660
Commit point reached, 8660 Request Sequences got updated.
Array count, 4001 Request Sequences got
2434
sql%rowcount is the number of rows updated by the whole forall .. update statement, not the number of times FORALL executed DML. Single update can affect many rows.
You can check also SQL%BULK_ROWCOUNT pseudo collection, it contains the number of rows affected for each DML statement executed by FORALL and you will see, that for some values v_reqseqArray(idx) it updated many rows.
Also bear in mind that your transaction sees your updates: cursor contains old values, the table contains updated, and UPDATE can see updates done in the same transaction.
BTW this is classic fetch across commit example.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the PEN_TIPO column of a table, this column can have values 0 and 2, and in the report depending on the filter I apply the condition as follows:
declare #PEN_TIPO int = 0
(A.PEN_TIPO = #PEN_TIPO OR #PEN_TIPO = 0)
However, it will have a condition that I do not need to filter this field, ie I have to get 0 and 2 from the PEN_TIPO column.
How can I apply this filter?
It sounds like you're describing, what is commonly referred to as an optional parameter.
If the user enters a parameter value, they want to filter based on that, if not, ignore it altogether.
It would typically look something like this:
DECLARE #PEN_TIPO INT;
(A.PEN_TIPO = #PEN_TIPO OR #PEN_TIPO IS NULL)
OPTION(RECOMPILE);
Please note that I added "OPTION(RECOMPILE)" to the end of the query.
You'll want to add this to you query too, so that the optimizer can create an optimized plan based on the chosen parameter value.
Are you trying to do this ?
DECLARE #PEN_TIPO INT = NULL
SELECT *
FROM TableName
WHERE
A.PEN_TIPO = ISNULL(#PEN_TIPO, PEN_TIPO)
When #PEN_TIPO = NULL then A.PEN_TIPO = A.PEN_TIPO which will bring everything.
This is usually handled with a similar conditional where clause using NULL, but you code would if you defaulted the value to 1 or another value.
In the below proc, we default the parameter to NULL. It will remain NULL if your report / application doesn't pass in a value.
If it remains null, all rows are returned.
If a value is passed in that is 0 or 2, the filter is applied.
If a value is passed in that isn't 0 or 2, an error is raised.
Here's the proc.
create proc myProc (#PEN_TIPO int = NULL)
as
if (#PEN_TIPO IS NOT NULL) or (#PEN_TIPO NOT IN (0,2))
begin
raiserror('Invalid parameter',16,1)
end
SELECT A.*
FROM SomeTable A
WHERE (A.PEN_TIPO = #PEN_TIPO OR #PEN_TIPO IS NULL)
Aaron Bertrand has an in-depth post on these Kitchen Sink type queries.
This question already has answers here:
Parameterize an SQL IN clause
(41 answers)
IN Operator in OLEDB
(2 answers)
Closed 4 years ago.
I'm trying to make this sample working but it fails every time and displays the bad response. Can you please explain to me why that is the case? Thanks
DECLARE #CARACS NVARCHAR(max) = 'DLV, DLC'
SELECT
CASE
WHEN 'DLV' IN (#CARACS)
THEN 'GOOD'
ELSE 'BAD'
END
This is the problem... DECLARE #CARACS NVARCHAR(max) = 'DLV, DLC' .
Only way this would work is splitting it up... because DLV will never equal 'DLV, DLC', to SQL Server 'DLV, DLC' is a single string.
DECLARE #CARACS1 NVARCHAR(max) = 'DLV', #CARACS2 NVARCHAR(max) = 'DLC'
SELECT
CASE
WHEN 'DLV' = #CARACS1 OR 'DLV' = #CARACS2
THEN 'GOOD'
ELSE 'BAD'
END
Here are 2 queries, if you are able to find the difference between them, then you will understand why your query is returning 'Bad' value:
SELECT
CASE
WHEN 'DLV' IN ('DLV, DLC')
THEN 'GOOD'
ELSE 'BAD' END
SELECT
CASE
WHEN 'DLV' IN ('DLV', 'DLC')
THEN 'GOOD'
ELSE 'BAD' END
Explanation:
'DLV,DLC' - is one value.
'DLV', 'DLC' - two values.
So, turn 'DLV,DLC' into several individual values, you need to use splitter function.
(I used Jeff Moden's splitter - DelimitedSplit8K)
DECLARE #CARACS VARCHAR(8000) = 'DLV,DLC'
SELECT
CASE
WHEN 'DLV' IN (select * from dbo.DelimitedSplit8K(#CARACS,','))
THEN 'GOOD'
ELSE 'BAD'
END
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Declare #Random int = 1, #Bool bit = 0;
WHILE (#Bool = 0)
BEGIN
SET #Random = ROUND(RAND()*(SELECT MAX(CharID) FROM SRO_VT_SHARD_INIT.dbo._Char where LastLogout < DATEADD(DAY, -3, CURRENT_TIMESTAMP),0)
IF exists (SELECT CharID FROM SRO_VT_SHARD_INIT.dbo._Char WHERE CharID = #Random)
BEGIN
SET #Bool = 1 /*true*/
END
END
print #Random
It gives and error after the CURRENT_TIMESTAMP it says that there is an syntax error near the comma. If I remove the ,0 then the ROUND function doesn't have enough arguments. Someone?
Change
CURRENT_TIMESTAMP),0)
to
CURRENT_TIMESTAMP)),0)