Copy column from one table to another table with condition - sql-server

ALTER PROCEDURE [dbo].[Timesheet_update]
AS
BEGIN
DECLARE #sql2 nvarchar(max), #status nvarchar(1)
SET #sql2 = 'insert into s21022020 (s21_stfno) select m_stfno from master where m_status<>'D''
EXECUTE (#sql2)
END
EXECUTE Timesheet_update
Results in an error:
Msg 207, Level 16, State 1, Line 23
Invalid column name 'D'.
m_status column contain data =D

I don't understand why you feel the need to make this a dynamic SQL - just write the statement directly inside the stored procedure - like this:
ALTER PROCEDURE [dbo].[Timesheet_update]
AS
BEGIN
INSERT INTO s21022020 (s21_stfno)
SELECT m_stfno
FROM master
WHERE m_status <> 'D'
END

Related

TSQL Temporary Procedure: There is already an object named

A similar question has been answered, but only for temporary tables.
I want to use a temporary stored procedure, as per
-- test if folder exists, else create it
create procedure #mycheckcreatefolder
(
#rootfolder NVARCHAR(MAX),
#subfolder NVARCHAR (MAX)
)
as
BEGIN
declare #full_path as NVARCHAR(MAX)
declare #folder_exists as int
declare #file_results table
(file_exists int, file_is_a_directory int, parent_directory_exists int)
select #full_path = #rootfolder + #subfolder
insert into #file_results
(file_exists, file_is_a_directory, parent_directory_exists)
exec master.dbo.xp_fileexist #full_path
select #folder_exists = file_is_a_directory
from #file_results
--script to create directory
if #folder_exists = 0
begin
print 'Directory does not exist, creating new one'
EXECUTE master.dbo.xp_create_subdir #full_path
print #full_path + 'created on' + ##servername
end
else
print 'Directory already exists'
END;
GO
-- use the stored procedure
but when I execute the query repeatedly I get
Msg 2714, Level 16, State 3, Procedure #mycheckcreatefolder, Line 30
There is already an object named '#mycheckcreatefolder' in the database.
Line 30 is the "print 'Directory already exists'".
I have removed all code below that, the error still pops up.
Also adding
drop procedure #mycheckcreatefolder
at the end does not help, as does a conditional drop in the beginning, since then I get the error that the "create procedure" needs to be the first instruction.
What is wrong?
if object_id('tempdb..#sp_today') is not null drop proc #sp_today;
go
create proc #sp_today as
select getdate() as dt;
You should search for it in tempdb, not in current db
IF OBJECT_ID('tempdb..#mycheckcreatefolder') IS NOT NULL
DROP PROCEDURE dbo.#mycheckcreatefolder
GO
CREATE PROCEDURE dbo.#mycheckcreatefolder
AS
PRINT 'test';

Reversing COLUMN DATA by dynamically passing table name and Column Name

I want to reverse a COLUMN DATA dynamically by passing table name and Column Name
DECLARE #TABLE_NAME NVARCHAR(300) -- to pass table name dynamically
DECLARE #COLUMN_NAME NVARCHAR(300) -- to pass column name dynamically
SET #TABLE_NAME = 'TEST1' -- Passing table name
SET #COLUMN_NAME = 'SSN' -- Passing column Name
DECLARE #OUTPUT NVARCHAR(MAX) -- to pass the selected column data to update
SET #OUTPUT = 'SELECT'+' '+#COLUMN_NAME+' '+'FROM'+#TABLE_NAME
DECLARE #UPDATE_EXEC NVARCHAR(MAX) -- To exec the Final update result
SET #UPDATE_EXEC = 'UPDATE ['+#TABLE_Name + ']'+'SET'+' '+#COLUMN_NAME+
'=REVERSE(EXEC (#output))'
EXEC(#UPDATE_EXEC)
But I'm getting these errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'EXEC'.
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "#output"
You are trying to put EXEC in your update query, its not allowed. EXEC is use to execute the query, it can't be part of any query.
To reverse any column in your table you can do so like below,
SET #UPDATE_EXEC = 'UPDATE ['+#TABLE_Name + ']'+'SET'+' '+#COLUMN_NAME+ '=REVERSE('+#COLUMN_NAME+')';
EXEC(#UPDATE_EXEC);

Executing one stored-procedure from another throws error

My Stored procedure is like this
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE ph_GetAllStaffAddressByCamp
#Year INT,
#strYear VARCHAR(2) OUTPUT
AS
IF NULLIF(#Year, '') IS NULL
SET #Year = Exec [cs_GetCurrentYear] #strYear
SELECT DISTINCT [VolID], [CampID], [VolFName] FROM [vStaffJobAndCamp]
where CampCode Like #Year
As you can see I am trying to execute [cs_GetCurrentYear] inside this procedure (added the procedure below)
ALTER PROCEDURE [dbo].[cs_GetCurrentYear]
#strYear VARCHAR(2) OUTPUT
AS
SELECT #strYear = Year FROM tblCurrentYear
But this throws an error on compile .
And it looks like this
Msg 156, Level 15, State 1, Procedure ph_GetAllStaffAddressByCamp, Line 8
Incorrect syntax near the keyword 'Exec'.
You can create a table, and insert into it the result of your SP. Try
DECLARE #TheYear TABLE
(
TheYear Varchar(2)
)
INSERT INTO #theYear (TheYear)
Exec [cs_GetCurrentYear] #strYear
SET #Year = (SELECT TOP(1) TheYear FROM #TheYear)
You can modify your second stored procedure to a stored function and call it from your first stored procedure. Read more here: How to call a scalar function in a stored procedure

Insert User Defined Data Table in a stored procedure

Below is the stored proc i have so far I keep getting this error when executed:
Msg 137, Level 16, State 1, Procedure db_recession_band_dates_save, Line 18
Must declare the scalar variable "#dates".
ALTER PROCEDURE [dbo].[Dates_Save]
#Loc VARCHAR(75),
#dates StartEndDateType READONLY
AS
BEGIN
DECLARE #id int
SELECT #id = MYINTFIELD FROM date_locations
IF #id IS NULL
BEGIN
INSERT INTO db_recession_bands VALUES (#loc)
SET #id = ##IDENTITY
END
INSERT INTO db_recession_band_dates VALUES (#id,#dates)
END
if StartEndDateType is a user defined table type then you treat it as if it were a table.
Change this:
INSERT INTO db_recession_band_dates VALUES (#id,#dates)
Into something like
INSERT INTO db_recession_band_dates (<COLUMN LIST>) -- don't do blind inserts it will hurt you at some point in the future
SELECT #id, <COLUMN LIST>
FROM #dates

EXEC select query not working with temp table SQL Server 2005

I have this SQL Server 2005 Puzzle
The following code doesn't work
DECLARE #tmp TABLE (ID int IDENTITY PRIMARY KEY , strDateTime varchar(50))
INSERT INTO #tmp VALUES('1/2/13')
DECLARE #x varchar(1000)
SET #x = 'SELECT * FROM ' + #tmp ;
EXEC (#x)
I get the following error
Msg 137, Level 15, State 2, Line 5
Must declare the scalar variable "#tmp".
If I do this
SET #x = 'SELECT * FROM #tmp ' ;
I get this error
Msg 1087, Level 15, State 2, Line 1
Must declare the table variable "#tmp".
Well what I was really trying to do is passing dynamic column name to a select statement and I found that I didn't like that either
Can someone guide me what I'm doing wrong
Thanks
A table variable is only valid in the scope within which it is created:
A table variable behaves like a local variable. It has a well-defined
scope. This is the function, stored procedure, or batch that it is
declared in.
To do this, you'll need to use a temporary table:
CREATE TABLE #tmp (ID int IDENTITY PRIMARY KEY , strDateTime varchar(50))
INSERT INTO #tmp VALUES('1/2/13')
DECLARE #x varchar(1000)
SET #x = 'SELECT * FROM #tmp';
EXEC (#x)

Resources