Saturday 23 June 2018

MSSQL - Create temp table from select query result

Watch this example on YouTube



Select *
Into #TempProduct
From Product

Select * From #TempProduct

MSSQL - Fix Error - Incorrect syntax near while updating table using Inner join



Watch this example on YouTube



To fix this error replace


Update Product p Inner Join Orders o on p.ProductID = o.ProductID set o.Qty = 25 where o.Qty = 20

with

Update  o Set o.Qty = 25
From Product p
 Inner Join Orders o on p.ProductID = o.ProductID  where o.Qty = 20

Saturday 16 June 2018

MSSQL - Fix Error - Incorrect syntax near the keyword 'Exec'.

Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'Exec'.

Watch this example on YouTube



I am trying to insert records from results of stored procedure

 to fix it replace

select * into #TemplTable
from Exec [SelectProducts]
select * FROM #TemplTable

with


IF OBJECT_ID('tempdb..#TemplTable') IS NOT NULL
DROP TABLE #TemplTable

CREATE TABLE #TemplTable (ProductID int, ProdName varchar(max))
Insert Into #TemplTable
Exec [SelectProducts]

select * FROM #TemplTable

IF OBJECT_ID('tempdb..#TemplTable') IS NOT NULL
DROP TABLE #TemplTable







MSSQL - Sql server - insert into table if record doesn't exist

Watch on YouTube


Select * From Orders

If not exists (Select * From Orders where OrderID = 10)
Begin
    Insert Into Orders (OrderID, ProductID, Qty, test) Values(10, 1, 20,1)
End

Select * From Orders

MSSQL - Insert results from stored procedure into temp table

Watch this example on YouTube





Create Table #TemplTable (ProductID int, ProdName varchar(max))
Insert into #TemplTable
Exec [SelectProducts]

select * FROM #TemplTable


stored procedure looks like this:
ALTER PROCEDURE [dbo].[SelectProducts]
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT ProductID, Name FROM Product
END

MSSQL - Fix Error - No item by the name of 'schema.Orders' could be found in the current database 'Company', given that @itemtype was input as '(null)'.

Watch this example on YouTube


Msg 15225, Level 11, State 1, Procedure sp_rename, Line 418 [Batch Start Line 0]
No item by the name of 'schema.Orders' could be found in the current database 'Company', given that @itemtype was input as '(null)'.


To Fix is replace

exec sp_rename 'schema.Orders', 'Orders2'

With

exec sp_rename 'Orders', 'Orders2'