how to set two Variable with one select in sql - sql

i have two Variable type in t-sql and want set they in sql. i use this code:
DECLARE #Id int
DECLARE #Name nvarchar(100)
set #Id, #Name = (Select Id, Name From Member Where username = #username)
but this not working, and i not want use this code:
set #Id = (Select Id From Member Where username = #username)
set #Name = (Select Name From Member Where username = #username)
i have use one select.

Here's the right syntax for what you want to do:
DECLARE #Id int
DECLARE #Name nvarchar(100)
SELECT #Id = Id, #Name = Name
From Member
Where username = #username

Related

Procedure to rename a list of procedure, only renames the last procedure in the list

I created a procedure where I could pass a list of procedure names separated by a comma, and then I would like to rename all the procedures from the list by adding a custom suffix in the end.
But I am facing an issue where it would only rename the last item from the list and ignore all the rest. I don't know why it would not rename each one from table variable list since I am calling sp_rename on each item in the list.
Also, I was able to print each loop with raiseerror()
Here's the complete procedure that I written.
CREATE PROCEDURE [dbo].[Util_ProcRename]
#ProcNameListWithComma VARCHAR(MAX)
AS
BEGIN
DECLARE #ID INT
SET #ID = 1;
DECLARE #oldname varchar(200)
DECLARE #newname varchar(200);
DECLARE #NOTUSED VARCHAR(20)
SET #NOTUSED = '_NOTUSED_'+REPLACE(CONVERT(varchar(20), GETDATE(), 101),'/','')
DECLARE #ProList AS TABLE (
ID INT IDENTITY,
ProcName VARCHAR(200),
NewProcName VARCHAR(200)
);
--Get proc names from a list into a table with ID to use in While loop
INSERT INTO #ProList (ProcName)
SELECT P AS ProcName
FROM dbo.SplitText(#ProcNameListWithComma,',')
WHILE (#id <= (SELECT MAX(ID) FROM #ProList))
BEGIN
SET #oldname = (SELECT ProcName FROM #ProList WHERE ID = #ID)
SET #newname = #oldname + #NOTUSED
EXEC p_rename #oldname, #newname
SET #ID = #ID + 1
END
UPDATE p
SET NewProcName = p.ProcName+#NOTUSED
FROM #ProList p
WHERE ID = ID
--TO see the list that got renamed
SELECT
a.Name AS [NewName],
a.type
FROM
dbo.sysobjects a
WHERE
name IN (SELECT NewProcName FROM #ProList)
END
GO
Despite the indentation, you are doing the update after the loop. So, it is only called once.
Move the END to after the UPDATE.

Sql Server Stored Procedure Case When in Update

I am new in stored procedures.
ALTER PROCEDURE [dbo].[SP_MY_STORE_PROCEDURED]
(
#ID INT,
#NAME VARCHAR(50)
)
AS
BEGIN
UPDATE MY_TABLE
SET
WHEN
(( ID ) > 1)
THEN
ID=#ID
,
NAME = #NAME
END
I try to use when then for update my ID and Name
If Id is greater than 1 i want to update otherwise no update.
How can i do it ms sql?
Any help will be appreciated.
Thanks.
I think this is what you are after:
ALTER PROCEDURE [dbo].[SP_MY_STORE_PROCEDURED]
(
#ID INT,
#NAME VARCHAR(50)
)
AS
BEGIN
UPDATE MY_TABLE
SET NAME = #NAME
WHERE ID = #ID
END
You do not need to check ID>1 since you are checking the equality with #ID. If you want to be sure that this doesn't happen if #ID <=1 then you may try the following:
ALTER PROCEDURE [dbo].[SP_MY_STORE_PROCEDURED]
(
#ID INT,
#NAME VARCHAR(50)
)
AS
BEGIN
IF #ID > 1
UPDATE MY_TABLE
SET NAME = #NAME
WHERE ID = #ID
END
I'm not sure exactly what you're trying to update. Are you trying to change the name on a user record with id = #ID?
ALTER PROCEDURE [dbo].[SP_MY_STORE_PROCEDURED]
(
#ID INT,
#NAME VARCHAR(50)
)
AS
BEGIN
UPDATE MY_TABLE
SET Name = #Name
WHERE Id = #ID and #ID > 1
END
This should do it.
ALTER PROCEDURE [dbo].[SP_MY_STORED_PROCEDURE] ( #ID INT, #NAME VARCHAR(50) )
AS
BEGIN
IF #ID > 1
UPDATE MY_TABLE
SET Name = #Name
WHERE Id = #ID
END

Put Where Clause in a parameter

I would like to use a where clause in a parameter. I already tried the following code but it doesnt work. the last line us red underlined, i reckon that i have to bind the parameter to the select command somehow. Would be great if anyone could help me with it.
Begin
declare #name varchar(MAX)
declare #x int
Set #x = 1
If #x = 1
BEGIN
SET #name = 'WHERE Username = Frank'
END
ELSE
BEGIN
SET #name = ''
END
END
now use it in:
SELECT * FROM dbo.person #name
Here is a typical way to have optional parameters in a query:
declare #UserName varchar(255) = 'Frank'
select *
from person p
where (#UserName is null or UserName = #UserName)
If you set the variable to NULL then all users are chosen. If you set it to a value, then only that user is chosen.
Looks like you want an optional Stored Procedure parameter to either pull rows with the parameter value or pull all rows:
CREATE PROCEDURE foo
#name AS VARCHAR(MAX) = NULL
AS
BEGIN
SELECT * FROM dbo.person WHERE Username = #name OR #name IS NULL
END
GO

need two return value from a stored procedure in c#

I want to call a Stored Procedure which takes two parameters(username & password) and returns two values of type int( 'result' and 'group' ) 'result' for checking whether the user is valid or not and 'group' simply shows the group number of customers
there are three type of groups
group#1 : admin users
group#2 : custom users
grpup#3 : special users
below is my current code which only returns result
ALTER PROCEDURE [dbo].[suserpass]
#username Varchar(50),
#password varchar(50),
#result int OUTPUT,
#group int OUTPUT
AS
IF EXISTS (select [user] from [userpassTable] where [user] = #username and [pass]=#password)
set #result=1
else
set #result=0
thanks
you need do soemthing as below you need to set both output variable that will do your task .....
ALTER PROCEDURE [dbo].[suserpass]
#username Varchar(50),
#password varchar(50),
#result int OUTPUT,
#group int OUTPUT
AS
IF EXISTS (select [user] from [userpassTable] where [user] = #username and [pass]=#password)
begin
select #result=1
select #group = usergroup from [userpassTable] where [user] = #username and [pass]=#password
end
else
begin
select #result=0
select #group=0
end
I guess that, conceptually, you’re looking for something like this (haven’t tested the code):
ALTER PROCEDURE [dbo].[suserpass]
#username Varchar(50),
#password varchar(50),
#result int OUTPUT,
#group int OUTPUT
AS
IF EXISTS (select [user] from [userpassTable] where [user] = #username and [pass] = #password)
BEGIN
set #result = 1
set #group = (select (top 1) [groupType] from [userGroupTable] where [user] = #username)
END
else
set #result=0
Set both output parameters.
You can do it like this:
IF EXISTS (SELECT [user] FROM [userpassTable]
WHERE [user] = #username AND [pass] = #password)
BEGIN
SELECT #result=1 , #group = group
FROM [userpassTable]
WHERE [user] = #username
END
else
BEGIN
SELECT #result=0 , #group = 0 -- group 0 doesn't exist, used as user does exist
END
I have added BEGIN/END blocks for readability and to allow more than one statement.
In your C# both #result and #group should be set to output parameters.

Placing column values in variables using single SQL query

In SQL server, how can I place the value of more than one column in variables using one query?
Ex: my query is:
SELECT ET.ID,ET.Description,ET.DefaultTemplateText
FROM TBL_EMAILTEMPLATE ET
WHERE ET.NAME='OneWeekReminder'
I want to place the column values in variables.
You can use the following syntax:
Declare #id INT
Declare #desc VarChar(100)
Declare #template VarChar(100)
SELECT #id = ET.ID, #desc = ET.Description, #template = ET.DefaultTemplateText
FROM TBL_EMAILTEMPLATE ET
WHERE ET.NAME='OneWeekReminder'
declare the variables first then set them in the select clause.
declare
#ID int,
#Description varchar(10),
#DefaultTemplateText varchar(10)
select
#ID = ET.ID,
#Description = ET.Description,
#DefaultTemplateText = ET.DefaultTemplateText
from
TBL_EMAILTEMPLATE ET
where
ET.NAME = 'OneWeekReminder'
You can separate multiple assignments with a comma. For example:
declare #a varchar(50)
declare #b varchar(50)
select
#a = et.Description
, #b = et.DefaultTemplateText
from YourTable
Assuming only one row,
SELECT #id = ET.ID, #Description = ET.Description, #DefaultTemplateText = ET.DefaultTemplateText
FROM TBL_EMAILTEMPLATE ET
WHERE ET.NAME='OneWeekReminder'