I got request in SQL server as stating below [closed] - sql-server

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
For example I have table like
Id name age
01 Praveen 20
02 mahesh 40
03 phani 50
I want see output like
01,02,03

Use For xml path() trick to do this. Considering that ID is of integer type else remove the convert function
SELECT Stuff((SELECT ',' + CONVERT(VARCHAR(20), Id)
FROM Yourtable
FOR xml path('')), 1, 1, '')

use cursor to fetch each id and concatenate them to a string
DECLARE cur cursor FAST_FORWARD READ_ONLY
FOR
SELECT id
FROM tbl
OPEN cur
DECLARE #x varchar(10)=''
DECLARE #d int
FETCH NEXT FROM cur INTO #d
WHILE ##FETCH_STATUS=0
BEGIN
SET #x=#x+convert(varchar(10),#d)+','
FETCH NEXT FROM cur INTO #d
END
Close cur
DEALLOCATE cur
SELECT left(#x,len(#x)-1)

Related

What is the time complexity of if-else statement inside a while loop? [closed]

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 1 year ago.
Improve this question
I want to calculate the time complexity of if-else statement inside a while loop but I am not sure how.
If you want to measure the elapsed time in the BEGIN END block;
[![DECLARE #Counter INT
DECLARE #TimeStart AS DATETIME
DECLARE #TimeStop AS DATETIME
SET #Counter=1
WHILE ( #Counter <= 10)
BEGIN
PRINT 'The counter value is = ' + CONVERT(VARCHAR,#Counter)
IF #Counter %4 =0
BEGIN
SET #TimeStart=GETDATE()
WAITFOR DELAY '00:00:001';
END
SET #TimeStop =GETDATE()
PRINT 'The elapsed time is = ' + CAST(DATEDIFF(MICROSECOND,#TimeStart,#TimeStop) AS VARCHAR(1000)) + ' microsecond'
SET #TimeStart= NULL
SET #TimeStop= NULL
SET #Counter = #Counter + 1
END

How can I replace varchar to table SQL Server [closed]

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
[]
I want to replace these column numbers <1700>/(<1027>-<1018>-<1019>-<1022>-<1020>)
Node column name
I think you are looking for something like this:
DECLARE #source VARCHAR(2000) = '<1027>-<1018>';
DECLARE #result VARCHAR(2000) = #source;
DECLARE #YourTable TABLE (
[Data] INT
, [Replacement] BIGINT
);
INSERT INTO #YourTable ([Data], [Replacement])
VALUES
(1700, -251158)
,(1027, 1482018)
,(1018, 75451);
SELECT #result = REPLACE(#result,CONCAT('<',[Data],'>'), [Replacement])
FROM #YourTable
SELECT #result
-- OUTPUT: 1482018-75451

What's the purpose of SQL keyword “AS”? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am wondering about what AS Keyword do in SQL Server.
I know it's context when it is used on a query that it sets up an alias, however, I do not know what it means when it's used in stored procedures or User Defined Functions. Why is it needed before the begin keyword?
CREATE FUNCTION dbo.UDF_rangeoddeven( #start int, #end int, #oddeven nvarchar(4))
returns #table table
(
Number int
)
AS
BEGIN
IF #oddeven = 'even'
WHILE #start <= #end
BEGIN
IF #start%2 = 0
BEGIN
INSERT INTO #table
SELECT #start;
END
SET #start = #start + 1;
END
ELSE
WHILE #start <= #end
BEGIN
IF #start%2 = 1
BEGIN
INSERT INTO #table
SELECT #start;
END
SET #start = #start + 1;
END
RETURN
END
GO
It's a hunk of language syntax.
For stored functions / procedures (sps), common table expressions (CTEs), and views, AS works as part of the declaration syntax. Think of it this way: declare public_part AS private part.
The public part of an sp is its declaration: how to access it. The private part is the code. The same is true of a CTE:
WITH summary AS (
SELECT id, MAX(val) maxval, MIN(val) minval, SUM(val) sumval
FROM raw
GROUP BY ID
)
The grammar is reversed from the way it's used in SELECTs, in which it's declare expression AS name.
Why is it reversed? Maybe Dr. Stonebraker or Dr. Date know?
Some SQL dialects allow AS to be omitted in some contexts.

Find specific ResultSet in stored procedure using sql query or sp [closed]

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 one sp with multiple resultsets like
Create procedure spNAME
As
Begin
select * from t1
select * from t2
select * from t3
END
now i want to create one stored procedure to find result of 3 statement in sp
Re-write your proc something like this..
Create procedure spNAME
#RS INT = NULL
As
Begin
IF (#RS = 1)
BEGIN
select * from t1
END
IF (#RS = 2)
BEGIN
select * from t2
END
IF (#RS = 3)
BEGIN
select * from t3
END
END
Now pass different parameter values to proc depending on what result set you want to return
Exec spNAME --<-- No Result set
Exec spNAME 1 --<-- Result set 1
Exec spNAME 2 --<-- Result set 2
Exec spNAME 3 --<-- Result set 3

Create a stored procedure which will generate alphanumeric with auto increment [closed]

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 8 years ago.
Improve this question
I need to display a label field with system generated value i.e alphanumeric value.So
is it possible to create a stored procedure which will return alphanumeric value, with auto increment value every time that it is executed.
where the alpha value will be constant like "ABC" only the numeric value will be incremented.
For example:
1st time => ABC12345
2nd time => ABC12346
and so on.
Create FUNCTION instead of Stored Procedure
CREATE FUNCTION USF_GenerateNewID() RETURNS VARCHAR(10)
AS
BEGIN
DECLARE #Prefix VARCHAR(3) = 'ABC'
DECLARE #ID VARCHAR(10)
SET #ID = (SELECT TOP 1 ID FROM <TABLE> ORDER BY ID DESC)
SET #ID = #Prefix + CAST((CAST(REPLACE(#ID,#Prefix,'') AS INT) + 1) AS VARCHAR(10))
RETURN #ID
END

Resources