creating view and procedure for non existense table? - sql-server

I have one query regards the creating a procedure and view.
Simple :
Create Procedure usp_demoXYZ
As
Begin
Select * from Table1 -- This table is not present in current Database
End
After execute the above query, query is executed successfully and created procedure.
But same things we apply in time of creating View.
Create View Vw_demoXYZ
As
Select * from Table1 -- This table is not present in current Database
after that I m running above query , sql server is showing error..
Msg 208, Level 16, State 1, Procedure Vw_demoXYZ, Line 4
Invalid object name 'Table1'

Stored procedures use Deferred Name Resolution while views does not do that. Deferred Name Resolution means when you create a stored proc it only checks if syntax of the statements are correct and not if the objects/tables it is referring to are there in database. That only occurs at runtime when you execute stored proc.
More details can be found here :
https://technet.microsoft.com/en-us/library/ms190686(v=sql.105).aspx

Related

There is already an object named 'name' in the database - stored procedure error - SQL Server

I have two tables called timeus and usdpay in my SQL Server database. These two tables get updated every week.
I did small transformation and combined these two tables and created a new table called fluslabor.
I created fluslabor using the stored procedure shown here, and it is working:
CREATE PROCEDURE [dbo].[sp_uslabor]
AS
BEGIN
SET NOCOUNT ON
IF OBJECT_ID(N'dbo.fluslabor', N'U') IS NOT NULL
TRUNCATE TABLE [dbo].[fluslabor];
SELECT ut.employee_code
,ut.employee_name
,ut.department_desc
,ut.location_desc
,ut.hour
,ut.projects_code
,ut.in_effective_time
,ut.out_effective_time
,ut.date
,ut.id
,p.rate
,(p.rate * ut.hour ) as Labour_Cost
INTO fluslabor
FROM timeus ut
LEFT JOIN usdpay p ON (TRIM(ut.id) = TRIM(p.id) AND ut.date = p.date)
WHERE ut.projects_code NOT LIKE '0%'
END
Today I got new data updated in my two tables timeus and usdpay.
When I execute my stored procedure, SQL Server is throwing this error:
Msg 2714, Level 16, State 6, Procedure SP_uslabor, Line 12 [Batch Start Line 38]
There is already an object named 'fluslabor' in the database.
I need to truncate my table every time and load the new data. I checked the similar post, they said to use drop table option. I don't want to drop the table, just want to truncate and execute the procedure
Can anyone advise what is the issue here please?
The problem here is that the table fluslabor already exists in the database. what you are trying above the insert is checking the object existence and then truncating the same
There are two possible approaches that you can try here.
Instead if the TRUNCATE do a DROP TABLE. But This will also remove the existing user permissions on the table if you have provided specific custom access to the table to any of the users
IF OBJECT_ID(N'dbo.fluslabor', N'U') IS NOT NULL DROP TABLE [dbo].[fluslabor];
The safest approach will be change the SELECT .. INTO statement and convert it into INSERT INTO like this
INSERT INTO fluslabor ( <List your Destination columns> ) SELECT <List your Source columns> FROM <Source Query>
the 2nd approach will have the records loaded along with keeping all the existing permissions
IF EXISTS (SELECT 1 FROM sys.objects WHERE type = 'P' AND name = 'your SP name')
BEGIN
DROP PROCEDURE "your SP name";
END
GO
CREATE PROCEDURE "your SP name"
AS
BEGIN
.
.
.
.
try this one I guess this will help you.

How do I select from a linked temp table, into a local temp table?

I'm currently calling a stored procedure on a linked server. This stored procedure selects data into a temporary table on this linked server. I'm then attempting to select this data into a temporary table on the local server, so that I can manipulate the data and pull it into various tables.
If I manually run the stored procedure on the server, I'm able to run the second part of the query (--SELECT DATA FROM TEMP TABLE). However, although I'm able to successfully call the stored procedure via the first part of my query, when it gets to the second part, I get the below errors:
Msg 8180, Level 16, State 1, Line 66
Statement(s) could not be prepared.
Msg 208, Level 16, State 1, Line 66
Invalid object name '##Sales'.
Is there another method I could use here? SSIS isn't an option right now, the requirement is to code this via T-SQL.
--CALL STORED PROC FOR SALES DATA
DECLARE #RunSalesStoredProcSQL VARCHAR(1000);
SET #RunSalesStoredProcSQL = 'EXEC [SERVER\INSTANCE].[DATABASE].[dbo].[Extract_Sales_Data]';
EXEC (RunSalesStoredProcSQL) AT [SERVER\INSTANCE];
Print ‘Sales Procedure Executed';
--SELECT DATA FROM TEMP TABLE
SELECT *
INTO ##TempTable
FROM OPENQUERY([SERVER\INSTANCE], 'SELECT * FROM dbo.##Sales');
Print 'Data Selected';
You could return the data from linked server procedure as a simple select, not selecting it in a temp table, and then you can do the following on the local server:
INSERT INTO ##TempTable(ColNames)
EXECUTE [SERVER\INSTANCE].[DATABASE].[dbo].[Extract_Sales_Data]
--select data
SELECT * FROM ##TempTable

Update SP called from Azure with input table parameter

I'm new in Azure and I've inherited a project with no documentation. I need to insert a new colum in some tables and I've modified every dataset in Azure but some SP are called from Azure in the pipeline with input table parameters and I don't know how to modify this SP to insert the new colum.
This is my code:
ALTER PROCEDURE [dbo].[spLoad] #azure_input_table [dbo].[InputTable] READONLY
INSERT [dbo].[Table]
columns,
new_column
SELECT
columns,
new_column
FROM
#azure_input_table
But I'm getting the following error:
Msg 207, Level 16, State 1, Procedure spLoad, Line 74 [Batch Start Line 6]
Invalid column name 'new_column'.
Beacuse I don't know how to refresh the input table parameter to modify mi SP through SSMS. I've modified all the physical tables but I don't know how to modify this SP with Azure input table parameters.
Thanks in advance!
Regards!
That is a table-valued parameter. Check out the user-defined table types. [dbo].[InputTable] does not have the column. You should just create a new user-defined table type to replace this one and adjust dependencies (like this stored procedure) to use the newly created type.

i have create a basic store procedure..when i m changing in this and execute it give me error that this name of SP already exit

This is my code
USE AccountSystemTraining
GO
CREATE PROCEDURE dbo.precetics
AS
SELECT * FROM Department where id=1
GO
error:
Msg 2714, Level 16, State 3, Procedure precetics, Line 4 There is
already an object named 'precetics' in the database.
If the procedure already exists and you need to change it then you may need to use ALTER instead of CREATE:
ALTER PROCEDURE dbo.precetics AS SELECT * FROM Department where id=1 GO
Note: From SQL Server 2016 you can do
CREATE OR ALTER PROCEDURE dbo.precetics AS SELECT * FROM Department where id=1 GO
which will always work without erroring regardless the procedure exists or not. In previous SQL server you can go around the issue by testing for existence first.

Return Stored Proc Results in CTE

Is it possible for me to call a stored proc into a CTE. I have a login to our reporting DB that is only RO. I have write access to our UAT but would like to query live data.
So can I use a stored proc in a CTE?
with clientOwes as (
exec des_Batch_GetApplicationClientOwesList
)
select a.des_applicationnumber
from des_heapplicationset a
where a.des_heapplicationid in (select applicationid from clientowes)
result was: Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'exec'.
Answer adapted from dialogue in comments:
You can use a stored procedure to populate a table variable, which Read Only access does allow you to create. You won't need to use OpenRowSet to populate it either. Just do:
INSERT INTO #MyTableVariable
EXEC MyStoredProcedure
I do this in a lot of places myself where I need to treat Stored Proc results as a table that I can JOIN or UNION with other tables.

Resources