Update SP called from Azure with input table parameter - sql-server

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.

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.

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.

creating view and procedure for non existense table?

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

SQL Server 2012 Stored Procedure to accept an IN parameter and do multiple inserts

I want to create a SQL Server 2012 Stored Procedure that will take an array from PHP as an IN parameter.
The table that the procedure will insert into is called dbo.Users and it will have three columns that I care about: createdby, userid, emailaddress
The array will include userid and email address. It call will include one OR more records to insert into the Users table.
How do I get the procedure to accept an array and how do I turn the array into multiple insert statements into my Users table? Do I need to use table value parameters? I heard of them but haven't used one before.
Thank you!
You can use XML parameter to pass a set / list(s) of related data to a stored procedure, or you can use Table valued parameter.

Query to create a new table to a specific database in SQL?

I have 3 databases sytemdatabases,smoketest and learnqueries .
Every time i write and execute create table query (create table tablename (colname datatype(size))), a new table is created in the systemdatabases.
I need it to be created to smoketest database.
I tried on this query (create table smoketest.newtablename(number int,name varchar(50));
It is showing an error (Msg 2760, Level 16, State 1, Line 1
The specified schema name "smoketest" either does not exist or you do not have permission to use it.) when i executed it.
It says 2 chances
Database does not exist - but the database exists
Does not have permission - what does it mean -how to set permission to create a
new table to a specific database
Pls help
The 4 part 'dot' notation in Sql Server for tables is
Server.Database.Schema.Object
So you would need to create the table with at least 3 parts if smoketest is not the current database on your connection, e.g. if you are on master, and assuming you want the new table in schema dbo:
create table smoketest.dbo.Tablename(ID INT)
Alternatively, switch to the smoketest database and create the table with 1 or 2 part naming:
use smoketest
GO
create table dbo.Tablename(ID INT)
GO
USE smoketest
GO
create table newtablename(number int,name varchar(50));
If you have a permission issue, check out this SQL Server 2008: how do I grant privileges to a username?
Hope this helps.

Resources