SQL Server not accepting DECLARE in INTO Statement - sql-server-2008

I'm somewhat new to SQL Server and am trying to use a while loop inside of an INTO statement, but for some reason SQL Server is not letting me DECLARE my variable inside of the INTO statement... seems to be okay w/ the while loop though, but I need to be able to DECLARE and define my variable to use in the while loop...any ideas?
--Calculations portion of query
SELECT
FNL.*
INTO
#Final_Calc
FROM
(
DECLARE #i INT
SET #i = 0
WHILE (#StartDte + #i <= #EndDte)
BEGIN
SET #i = #i + 1
SELECT
YEAR(ACD.WorkDte) AS WorkDte,
'LOB - ALL ACM' AS RptType,
'Month' AS RptLvl
FROM
#FinalResults ACD
WHERE
ACD.WorkDte BETWEEN #StartDte AND #StartDte + #i
GROUP BY
Year(ACD.WorkDte), ACD.LOB
END
GO) fnl
So, it is not liking DECLARE #i INT portion...

Related

MySQL stored procedure issue, generate custom ID's

I had a stored procedure for making custom ID, but it was for SQL Server and I tried to convert it to MySQL with help of an online tool, and I get errors and I need help from you guys :(
This stored procedure will produce ID codes for my users, like for admin (AD001, AD002,...etc), first, it will look if there is no value it will generate the first value like (AD001) if there is a value it will get the value and increment it by 1.
This is my SQL Server version of the procedure which works:
create procedure admin_code
as
begin
declare #max int, #no varchar(50), #value varchar(50)
select #max = isnull(max(cast(right(id_user, 3) as int)), 0) + 1
from admin_information
if #max < 100
select #no = 'AD' + right('00000' + cast(#max as varchar(50)), 3)
else if #max >= 100
select #no = 'AD' + right('0' + cast(#max as varchar(50)), 4)
if #max >= 1000
select #no = 'AD' + right('0' + cast(#max as varchar(50)), 5)
print #no
select #no
return 0
end
And this is for MySQL
create procedure admin_code()
sp_lbl:
begin
declare v_max int;
declare no varchar(50);
declare value varchar(50);
select v_max=ISNULL( max(cast(RIGHT(id_user,3)as int)),0)+1 into v_max from admin_information;
if v_max <100 then
set v_no=CONCAT('AD',RIGHT('00000'+CAST(v_max as varchar(50)),3));
elseif
v_max>=100 then
-- SQLINES LICENSE FOR EVALUATION USE ONLY
set v_no=CONCAT('AD',RIGHT('0'+CAST(v_max as varchar(50)),4));
end if;
if v_max>=1000 then
-- SQLINES LICENSE FOR EVALUATION USE ONLY
set v_no=CONCAT('AD',RIGHT('0'+CAST(v_max as varchar(50)),5))
print;
end if; v_no
-- SQLINES LICENSE FOR EVALUATION USE ONLY
select v_no;
LEAVE sp_lbl 0;
end;
This is the online tool I used:
http://www.sqlines.com/online
Error:

Is that correct way how to declare variable in SQL?

I am very new in SQL. I just want to create basic trigger. It seems there might the problem about variable declaration or assignment.
DECLARE #i INT = 0
DECLARE #j INT = 0
DECLARE #player_name VARCHAR(255), #player_team VARCHAR(255);
DECLARE #absence_list_length = SELECT COUNT(*) FROM nhl_absence;
WHILE #i < 30
BEGIN
SET #player_name = SELECT playoff FROM `nhl_standings` where id like #i
SET #player_team = SELECT skr FROM `nhl_standings` where id like #j
WHILE #j < #absence_list_length
BEGIN
SELECT user FROM `nhl_absence` where name like #player_name;
UPDATE nhl_absence SET team = #player_team;
SET #j = #j + 1;
END
SET #player_team = SELECT skr FROM `nhl_standings` where id like #i
UPDATE nhl_standings SET team = #player_team;
SET #i = #i + 1;
END
Error message:
#1064 - Something is wrong in your syntax near 'DECLARE #i INT = 0 DECLARE #j INT = 0 DECLARE #player_name VARCHAR(255), #play' on line 1
If this is for MSSQL (Microsoft SQL Server), I think you need to declare and initialize the variable with 2 separate lines of code:
Instead of:
DECLARE #i INT = 0
Try:
DECLARE #i INT
SET #i = 0
See Variables (Transact-SQL) for details.
local variables at contrary of user variables doesnt allow #.
https://dev.mysql.com/doc/refman/8.0/en/declare-local-variable.html
Also you assign value using DEFAULT or SET
SQL DEMO
CREATE PROCEDURE simpleproc ()
BEGIN
DECLARE i INT DEFAULT 5;
DECLARE j INT DEFAULT 10;
DECLARE player_name VARCHAR(255) DEFAULT 'JHON';
DECLARE player_team VARCHAR(255) DEFAULT 'BRONCOS';
DECLARE absence_list_length INT;
SET absence_list_length = (SELECT 5);
SELECT i, j, player_name, player_team, absence_list_length;
END;
OUTPUT

Is it possible to set the select list dynamically in MSSQL server2008?

I have a requirement to populate the select list dynamically, Could you please suggest any solution?
Example:
DECLARE #DATE CHAR
SET #DATE='E.TERMINATION_DATE'
--SET #DATE='E.HIRE_DATE'
SELECT E.ID,E.FIRSTNAME,E.LASTNAME, #DATE FROM DBO.EMPLOYEES E
Is it possible to set the column name in select query?
For some query i need to set the variable #DATE as TERMINATION DATE and for some query i need to set #DATE as E.HIREDATE.
When i will run the query based on the TERMINATION_DATE then i will comment in the other option.
or do you have any other workaround?
You can use the Dynamic sql. But you should read this blog to know about it.
DECLARE #DATE VARCHAR(100),
#SQL VARCHAR(MAX)
IF YOURCONDITION
BEGIN
SET #DATE='E.TERMINATION_DATE'
END
ELSE
BEGIN
SET #DATE='E.HIRE_DATE'
END
SET #SQL = 'SELECT E.ID,E.FIRSTNAME,E.LASTNAME,' + #DATE +' FROM DBO.EMPLOYEES E'
exec sp_executesql #SQL
you can use variables in select query but you should assign that query to some variable and execute that variable
because in SSMS at first it will checks(run time) then it executes. In our scenario the variable is assigning at execution only
so we need to assign it to variable as #coderofcode told
DECLARE #DATE VARCHAR(100),
#SQL VARCHAR(MAX)
SET #DATE='E.TERMINATION_DATE'
SET #SQL = 'SELECT E.ID,E.FIRSTNAME,E.LASTNAME,' + #DATE +' FROM DBO.EMPLOYEES E'
exec(#sql)
I was writing a function for which i need to use execution_date for some case and for some other case i need to use hire_date.
So i think i can now populate this things dynamically and it's a great invention for me. Thanks all specially Giorgi, coder of code and koushik.
I am using this pseudo code here.
--Setting the condition parameters
DECLARE #HIRE_DATE DATETIME,
SET #HIRE_DATE= SELECT MIN(HIRE_DATE) FROM E.EMPLOYEES WHERE E.PERSON_ID>30000
--based on condition setting the variable dynamically.
IF #HIRE_DATE IS NULL
BEGIN
SET #DATE='E.TERMINATION_DATE'
END
ELSE
BEGIN
SET #DATE='E.HIRE_DATE'
END
--setting the sql to execute.
SET #SQL = 'SELECT COUNT(E.NR) AS ACTIVITIES,E.CLI_FIRSTNAME,E.CLI_LASTNAME,E.DEBTORNR,' + #DATE +' FROM DBO.EMPLOYEES E
GROUP BY E.CLI_FIRSTNAME,E.CLI_LASTNAME,E.DEBTORNR,' + #DATE
exec(#sql)

Shuffle a string with mysql/sql

I was wondering, if there is some way to shuffle the letters of a string in mysql/sql, i.e. something like the pseudocode: SELECT SHUFFLE('abcdef')?
Couldn't find any from http://dev.mysql.com/doc/refman/5.0/en/string-functions.html and searching for it just seems to find solutions for shuffling results, not a string.
Here you go:
DELIMITER //
DROP FUNCTION IF EXISTS shuffle //
CREATE FUNCTION shuffle(
v_chars TEXT
)
RETURNS TEXT
NOT DETERMINISTIC -- multiple RAND()'s
NO SQL
SQL SECURITY INVOKER
COMMENT ''
BEGIN
DECLARE v_retval TEXT DEFAULT '';
DECLARE u_pos INT UNSIGNED;
DECLARE u INT UNSIGNED;
SET u = LENGTH(v_chars);
WHILE u > 0
DO
SET u_pos = 1 + FLOOR(RAND() * u);
SET v_retval = CONCAT(v_retval, MID(v_chars, u_pos, 1));
SET v_chars = CONCAT(LEFT(v_chars, u_pos - 1), MID(v_chars, u_pos + 1, u));
SET u = u - 1;
END WHILE;
RETURN v_retval;
END;
//
DELIMITER ;
SELECT shuffle('abcdef');
See sqlfiddle.com for the output.
Tested successfully with mariadb 10.1 (mysql 5.6 equivalent)
Edit: this solution is for Microsoft SQL Server.
As it's not allowed to use RAND() in user defined function, we create a view to use it later in our shuffle function:
CREATE VIEW randomView
AS
SELECT RAND() randomResult
GO
The actual shuffle function is as following:
CREATE FUNCTION shuffle(#string NVARCHAR(MAX))
RETURNS NVARCHAR(MAX) AS
BEGIN
DECLARE #pos INT
DECLARE #char CHAR(1)
DECLARE #shuffeld NVARCHAR(MAX)
DECLARE #random DECIMAL(18,18)
WHILE LEN(#string) > 0
BEGIN
SELECT #random = randomResult FROM randomView
SET #pos = (CONVERT(INT, #random*1000000) % LEN(#string)) + 1
SET #char = SUBSTRING(#string, #pos, 1)
SET #shuffeld = CONCAT(#shuffeld, #char)
SET #string = CONCAT(SUBSTRING(#string, 1, #pos-1), SUBSTRING(#string, #pos+1, LEN(#string)))
END
RETURN #shuffeld
END
Calling the function
DECLARE #string NVARCHAR(MAX) = 'abcdefghijklmnonpqrstuvwxyz0123456789!"ยง$%&/()='
SELECT dbo.shuffle(#string)
There is nothing in standard SQL - your best bet is probably to write a user defined function

Using #variable in mysql used as command in vb.net

I've used this type of query under VB.NET 2008, but everytime I run it, it always gives me a fatal error and it's telling me to declare the #variable I've used.
Below is the sample code:
select js.year, js.week, js.rem_balance,
case when js.rem_balance = 0
then #prev_rem_balance
else js.rem_balance
end as rem_balance_zero_or_prev,
#prev_rem_balance := js.rem_balance
from test_jos_stock js
inner join (SELECT #prev_rem_balance := 0) as t
order by year,week;
You need to declare the parameter before your select statement:
DECLARE #prev_rem_balance INT; --or whatever datatype it is
Then set it to be a specific value:
SET #prev_rem_balance = 1234;