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
Related
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 9 days ago.
Improve this question
I have created a stored procedure:
CREATE PROCEDURE TextStoreLogin
#user nvarchar(30),
#flag bit
AS
BEGIN
IF #user IS NOT NULL AND #flag = 0
SELECT *
FROM login u
WHERE u.UserName = #user
END
I want to execute accept parameter redundant store:
EXEC TextStoreLogin #user = 'MB139986', #flag = 0, #hau = '0';
Thank you very much.
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.
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
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)
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