DECLARE on spark sql - sql

Can someone give an idea on how would i DECLARE on spark sql coz i am doing a conversion of script from sql to spark sql.
DECLARE #MINID INT
SELECT #MINID = ISNULL(MIN(ID),3) FROM SAMPLETABLE
Thank you!
I have tried something like SET but it did not worked out for me.

Related

Is it possible to declare a Database as a variable?

I am working on data migration and want to be able to change my SourceDatabase without eding the code in multiple places is this possible? This is what I have tried from looking at some other posts but I must be doing something wrong
Declare #SourceDatabase VARCHAR(255)
Set #SourceDatabase = '[PrimaryTesting]';
Select LAid+Dfeid as DFE,SchoolName from #SourceDatabase.dbo.School;
I assume you're using MSSQL, in which case you'd have to build up the desired SQL string, then use the exec statement to run it:
declare #SourceDatabase VARCHAR(255) = '[PrimaryTesting]';
exec('select LAid+Dfeid as DFE,SchoolName from ' + #SourceDatabase + '.dbo.School;');
However, please be aware of the possible dangers of SQL injection when using this approach.

how to solve global temp table in windows azure

i am using global temp in my sql stored procedure when i upload into azure it is not working, can anyone please help me to solve this issue
please check the below code had problem
DECLARE #Q1 VARCHAR(MAX)
SET #Q1='SELECT * INTO ##FINAL FROM
(SELECT [ASSIGNEDTO],LEFT([MONTH],3) AS [MONTH],[COUNT]
FROM #ASSIGNEE_TICKETSTATUS)A
PIVOT(SUM([COUNT])
FOR [MONTH] IN('+#COLUMNHEADER +'))B'
PRINT #Q1
EXECUTE(#Q1)
SELECT * FROM ##FINAL
i can't write it as direct query it is not working when i wrote in directly, please anyone help

How to set a variable to the result of a sql query with a variable as a table name in SQL 2005

I'm currently having trouble writing a stored procedure and setting the value of a variable of type int to the results of a select statement with a variable as the tablename. I've looked at old threads and tried multiple methods, but no luck. If I'm not getting an error regarding the tablename, I end up getting an error with a variable conversion issue. I've been working on this for too long and any help would be appreciated. Below is a portion of my code. Thanks
DECLARE #BATCHNUMBER VARCHAR --value set in earlier code
DECLARE #ETABLE VARCHAR(50); --the table name
DECLARE #FIRSTDOCID INT;
SET #ETABLE = 'tablename_' + #BATCHNUMBER; --CREATE FIRST TABLE NAME
SELECT #FIRSTDOCID = MIN(D0CID) FROM #ETABLE
The error I get is: Must declare the table variable "#ETABLE"
You are trying to select from a VARCHAR, not a table. The only way to make this work is by using Dynamic SQL.
DECLARE #SQL NVARCHAR(250);
SET #SQL = 'SELECT #OUTPUT = MIN(D0CID) FROM ' + QuoteName(#ETABLE);
EXEC sp_executeSql #SQL, N'#output INT OUTPUT', #FIRSTDOCID OUTPUT;
SELECT #FIRSTDOCID;
However, I would not suggest using Dynamic SQL as this often leads to SQL injection.
You'll probably have to do something like use exec if you're dynamically building the query:
SET #QUERY = "SELECT" + ...etc.
exec(#QUERY)
Since ETABLE is a varchar, and not, as expected, a 'table variable'.

SQL Server CE Cast/Convert Double to String

This may seems like a stupid question for most, but it has been bugging me for quite a while.
How do we concat a string and an integer in SQL Server CE?
FYI, I am using Vb.Net and Visual Studio 2010 Query Builder.
I tried
SELECT CONVERT(VARCHAR(10), mi.qty) FROM MutationItem mi
and
SELECT CAST(qty AS VARCHAR(10)) AS Expr1 FROM MutationItem mi
and
SELECT CAST(qty AS VARCHAR) AS Expr1 FROM MutationItem mi
with no luck.
The latter gives me an error :
Error in list of function arguments: 'AS' not recognized. Unable to
parse query text.
Please give me a hand on this.
Thanks !
Sorry guys, it's my bad.
Or actually it's the wizard's bad.
So I tried to push it even though it gives me an error, and it worked eventually.
My guess is that the table relation designer cant replicate CAST or CONVERT, and then giving me an alert about that.
Cheers !
I skipped this problem Visual Studio create a function with the code which throws problems
for example this code sql on sybase
CREATE FUNCTION GetV(#cod NUMERIC(8))
RETURNS VARCHAR(20)
AS
BEGIN
DECLARE #result VARCHAR(20)
DECLARE #query VARCHAR(100)
SET #query = (SELECT p.v1+' '+cast(p.v2 AS VARCHAR)+'X'+cast(p.v3 AS VARCHAR) FROM table1 p WHERE p.cod=#cod)
IF(#query IS NULL OR #query = '' OR #query = ' ')
SET #result = 0
ELSE
SET #result = #query
RETURN #result
END
GO

Simple dynamic TSQL query syntax

This may be an easy answer but I've been staring at it for too long...
I have the following query that takes a stored procedure input parameter as a variable name and counts the records in that table. I'd like to retrieve the results of the dynamic statement (#toStartStr) into a variable (#toStart).
-- #tempTableName = SProc input parameter
DECLARE #toStartStr nvarchar(150);
DECLARE #toStart int;
SET #toStartStr = 'SELECT #toStart = COUNT(ID) FROM ' + #tempTableName;
EXEC(#toStartStr);
Right now, an error suggests that #toStart cannot be concatenated with the string SELECT, but this is the gist of what I want. Can anyone see what I'm doing wrong? Or suggest an alternative? FYI SQL 2008 R2. Thanks.
DECLARE #sql NVARCHAR(255);
DECLARE #toStart INT;
SET #sql = N'SELECT #toStart = COUNT(ID) FROM ' + QUOTENAME(#tempTableName);
EXEC sp_executesql #sql, N'#toStart INT OUTPUT', #toStart OUTPUT;
PRINT #toStart;
However there is a much easier and more efficient way to do this, if you're okay with ignoring current in-flight transactions (and you're using SQL Server 2005 or better - please specify the version when asking questions!).
DECLARE #toStart INT;
SELECT #toStart = SUM(rows)
FROM sys.partitions
WHERE [object_id] = OBJECT_ID(#tempTableName)
AND index_id IN (0,1);
PRINT #toStart;
Just for completeness, here is a solution for SQL Server 2000, which also doesn't require any special privileges (just connect and member of public):
DECLARE #toStart INT;
SELECT #toStart = [rows]
FROM sysindexes
WHERE id = OBJECT_ID(#tempTableName)
AND indid IN (0,1);
PRINT #toStart;
That said, if you're using a count to determine what the next ID might be, or something like that, I think you're approaching this the wrong way, since rows can be deleted and if it's an identity column values can be skipped due to rollbacks.