CREATE PROC PS_LOGIN(
#ID NVARCHAR(50),
#PSW NVARCHAR(50)
)
AS
BEGIN
SELECT * FROM Table_Users WHERE ID=#ID AND PSW=#PSW
END
Try something like this....
IF OBJECT_ID('PS_LOGIN' , 'P') IS NOT NULL
DROP PROC PS_LOGIN
GO
CREATE PROC PS_LOGIN(
#ID NVARCHAR(50),
#PSW NVARCHAR(50)
)
AS
BEGIN
SELECT * FROM Table_Users WHERE ID=#ID AND PSW=#PSW
END
GO
Related
CREATE PROC CreateOrUpdate
#ID INT,
#NRIC VARCHAR(50),
#Name VARCHAR(50),
#Description VARCHAR(50),
#Location1 VARCHAR(50)
AS
BEGIN
IF (#ID = 0)
BEGIN
INSERT INTO tblBlacklist (NRIC, Name, Description, createdatetime, Location1)
VALUES (#NRIC, #Name, #Description, GETDATE(), #Location1)
END
ELSE
BEGIN
UPDATE tblBlacklist
SET NRIC = #NRIC,
Name = #Name,
Description = #Description,
createdatetime = GETDATE()
WHERE ID = #ID
AND Location1 = #Location1
END
END
/****** Object: StoredProcedure [dbo].[ContactViewAll] Script Date: 15-
Apr-2017 7:39:13 AM ******/
CREATE PROC ViewAll
#Location1 VARCHAR(50)
AS
BEGIN
SELECT *
FROM tblBlacklist
WHERE Location1 = #Location1
END
I cannot see any errors within my coding. The error message is
Msg 156, Level 15, State 1, Procedure CreateOrUpdate, Line 28 [Batch Start Line 0]
Incorrect syntax near the keyword 'PROC'
Please help me thanks.
One suggestion is to add a go between your two create statements, and remove the system comment:
CREATE PROC CreateOrUpdate
#ID int,
#NRIC varchar(50),
#Name varchar(50),
#Description varchar(50),
#Location1 varchar(50)
AS
BEGIN
IF(#ID=0)
BEGIN
INSERT INTO tblBlacklist(NRIC,Name,Description,createdatetime,Location1)
VALUES(#NRIC,#Name,#Description,getdate(),#Location1)
END
ELSE
BEGIN
UPDATE tblBlacklist
SET
NRIC = #NRIC,
Name = #Name,
Description = #Description,
createdatetime = getdate()
WHERE ID= #ID and Location1=#Location1
END
END
GO
CREATE PROC ViewAll
#Location1 varchar(50)
AS
BEGIN
SELECT *
FROM tblBlacklist
WHERE Location1 = #Location1
END
GO
try surround your input parameters inside fitst brace ()
declare #Name varchar(100),#sql varchar(400),#sql2 varchar(400),#sql3 varchar(400) ,#sql4 varchar(400) , #psw varchar(10)
--declare #psw varchar(10) = 'Traf#12345'
declare #T table (Name varchar(100))
insert into #T values
('SampleUser1'),
('SampleUser1')
While exists(select * from #T)
begin
select top 1 #Name = name from #T
IF NOT EXISTS (SELECT [name] FROM [sys].[sql_logins] WHERE [name] = #Name)
BEGIN
SET #psw = 'Traf#123'
SET #sql = 'CREATE LOGIN '+'['+#name+']'+' WITH PASSWORD ='+#psw
EXEC (#sql)
END
DELETE FROM #T WHERE Name = #Name
END
output:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near
'Traf#123'.
Please try the following changes:
declare #Name varchar(100),#sql varchar(400),#sql2 varchar(400),#sql3 varchar(400) ,#sql4 varchar(400)
declare #psw varchar(10) = 'Traf#12345';
declare #T table (Name varchar(100)) ;
insert into #T values
('SampleUser1'),
('SampleUser2')
While exists(select * from #T)
begin
select top 1 #Name = name from #T
IF NOT EXISTS (SELECT [name] FROM [sys].[sql_logins] WHERE [name] = #Name)
BEGIN
SET #sql = 'CREATE LOGIN '+'['+#name+']'+' WITH PASSWORD ='''+ #psw + '''; '
EXEC (#sql)
END
DELETE FROM #T WHERE Name = #Name
END
I have many tables that need ID scramblers, so:
CREATE PROCEDURE SP_generateUniqueID ( -- pass table here somehow -- )
AS
BEGIN
DECLARE #ID varchar(100) -- NEW ID.
DECLARE #isIDInUse tinyint -- BOOLEAN YES/NO.
SET #isIDInUse=1
WHILE(#isIDInUse=1) -- KEEP GENERATING TILL YOU FIND ONE:
BEGIN
SET #ID= dbo.generateID('aA1a1') -- GENERATES ID. doesn't matter how.
IF (#ID NOT IN (#passedTable)) -- DOES #ID EXIST ALREADY?
/*(SEARCHES THE PASSED TABLE! Which its size will be 1XN)*/
SET #isIDInUse=0 -- NO, YOU CAN USE.
END
RETURN #ID
END
I can't make the passing of the existing table go smoothly...
I want to be able to insert any table that uses IDs.
Any suggestion?
I would advise you REALLY look hard into better solutions for this issue. You will be hitting your table/index with every iteration of the new ID that you generate. What is wrong with an auto-incrementing integer value:
create table IDs (ID int identity(1,1))
(also, SQL Server has bit data types for boolean values. No need for your tinyint)
That aside, the only way I think you can do this your way is with dynamic SQL. Using the script below you should be able to see how you can pass in your schema.table to the stored procedure and within the procedure define your ID to be inserted in to the checking loop:
create table a(ID nvarchar(100)) insert into a values('1'),('2'),('3'),('4'),('5')
create table b(ID nvarchar(100)) insert into b values('6'),('7'),('8'),('9'),('10')
declare #Table nvarchar(100) = 'dbo.a'
declare #ID nvarchar(100) = '6'
declare #IDinUse bit = 0
declare #sql nvarchar(max) = 'if exists(select ID from ' + #Table + ' where ID = #ID) select #IDinUse = 1 else select #IDinUse = 0'
exec sp_executesql #sql, N'#ID nvarchar(100), #IDinUse bit output', #ID = #ID, #IDinUse = #IDinUse output
select #IDinUse as IDinUse
go
declare #Table nvarchar(100) = 'dbo.b'
declare #ID nvarchar(100) = '6'
declare #IDinUse bit = 0
declare #sql nvarchar(max) = 'if exists(select ID from ' + #Table + ' where ID = #ID) select #IDinUse = 1 else select #IDinUse = 0'
exec sp_executesql #sql, N'#ID nvarchar(100), #IDinUse bit output', #ID = #ID, #IDinUse = #IDinUse output
select #IDinUse as IDinUse
I am writing a procedure in SQL Server to insert or update records.
The update part of the code is working fine but when I am executing it for inserting, duplicate entries are inserted into the table.
I created the primary key to avoid this error but after creating that I am not able to insert any single record.
Here is the code :
Alter Procedure test_case
#id int,
#name nvarchar(20)
AS
If exists (Select t_id from testing2 where t_id = #id)
begin
update testing2
set t_id = #id, t_name = #name
where t_id = #id
end
else
begin
insert into testing2 (t_id, t_name, last_date, hard)
select
#id, #name, convert(date, getdate()), 'null'
from test
end
On executing it is showing 2 rows affected
You do not require test table in the select query
insert into testing2 (t_id, t_name, last_date, hard)
select
#id as t_id, #name as t_name, convert(date, getdate()) as last_date, 'null' as hard
is enough
I like to break functionality into smaller parts because it helps me to manage code better.
Maybe this is not a good example since it is pretty simple but I will write it anyway.
Create Procedure Testing2_InsertData (
#id int,
#name nvarchar(20)
) As
Set NoCount On
Insert Into testing2
(t_id, t_name, last_date, hard)
Values
( #id, #name, GetDate(), null )
Go
Create Procedure Testing2_UpdateData (
#id int,
#name nvarchar(20)
) As
Set NoCount On
Update testing2 Set
t_name = #name --, maybe last_date = GetDate()
Where ( t_id = #id )
Go
Create Procedure Testing2_SaveData (
#id int,
#name nvarchar(20)
) As
Set NoCount On
If ( Exists( Select t_id From testing2 Where ( t_id = #id ) ) )
Exec Testing2_UpdateData #id, #name
Else
Exec Testing2_InsertData #id, #name
Go
I am trying to write a recursive procedure that would delete the node and all it's children if they are such in the table. I tried doing the following
CREATE PROCEDURE DeleteFile
#FileID INTEGER,
#UserID VARCHAR(MAX)
AS
DELETE FROM [FileTree] WHERE [ID] = #FileID AND [UserID]=#UserID;
IF EXISTS(SELECT * FROM [FileTree] WHERE [ParentID] = #FileID AND [UserID]=#UserID)
BEGIN
DECLARE FileCursor CURSOR LOCAL FOR
SELECT [ID],[UserID] FROM [FileTree] WHERE [ParentID] = #FileID AND [UserID]=#UserID;
OPEN FileCursor
FETCH NEXT FROM FileCursor INTO #FileID , #UserID
WHILE ##FETCH_STATUS =0
BEGIN
EXEC DeleteFile #FileID,#UserID;
FETCH NEXT FROM FileCursor INTO #FileID , #UserID ;
END
END
ELSE
return
But for some reason this is not working. It deletes the node but the kids remain.
Table ,,design" .
CREATE TABLE [FileTree] (
[ID] INT IDENTITY NOT NULL,
[Name] VARCHAR (MAX) NOT NULL,
[ParentID] INT NULL,
[UserID] VARCHAR (MAX) NOT NULL
);
Can you please indicate the errors I made and suggest a working procedure ?
UPD: I made the cursor LOCAL and I am fetching one time before going into the while loop, it still does not delete all the children.
I think you have syntax problem.i fixed it like this
CREATE PROCEDURE DeleteFile
#FileID INTEGER,
#UserID VARCHAR(MAX)
AS
BEGAIN
DELETE FROM [FileTree] WHERE [ID] = #FileID AND [UserID]=#UserID;
IF EXISTS(SELECT * FROM [FileTree] WHERE [ParentID] = #FileID AND [UserID]=#UserID)
BEGIN
DECLARE FileCursor CURSOR LOCAL FOR
SELECT [ID],[UserID] FROM [FileTree] WHERE [ParentID] = #FileID AND [UserID]=#UserID;
OPEN FileCursor
FETCH NEXT FROM FileCursor INTO #FileID , #UserID
WHILE ##FETCH_STATUS =0
BEGIN
EXEC DeleteFile #FileID,#UserID;
FETCH NEXT FROM FileCursor INTO #FileID , #UserID ;
END
END
END
You can use a CTE and a temporary table:
WITH CTE (ID)
AS
(
SELECT ID FROM FileTree WHERE Id=#ID
UNION ALL
SELECT t.ID FROM FileTree t
INNER JOIN CTE c ON t.ParentId=c.Id
)
SELECT ID INTO #temp FROM CTE;
DELETE FROM FileTree WHERE ID IN(SELECT ID FROM #temp)
DROP TABLE #temp