intellisense not see global temp table - sql

I open a new query in ssms and create a global temp table. I then open a new query window in ssms and write sql to insert data in the fields. But in the new query window intellisense does not recognize the temporary table. The sql works fine and the data is inserted into the temp table and I can work with the temp table but without intellisense. I can return to the original query window where the table was created and intellisense work fine. I have tried refreshing intellisense, it doesn't work. Any suggestion will be appreciated.
Table code is
CREATE TABLE ##UserInfo
(
[UserId] int NOT NULL IDENTITY (1, 1),
[strEmail] varchar(50) NULL,
[strLastName] varchar(50) NULL,
[strFirstName] varchar(50) NULL,
)
Insert code
insert into ##Userinfo(strLastName,strFirstName)
select distinct POC_DATA.POC_LNAME, POC_DATA.POC_FNAME
from POC_DATA
The bold is the part intellisense does not recognize in the different query window

So far the only "trick" I have been able to come up with is to include the "CREATE TABLE" command in a block that will never execute, but intellisense will see, like so:
IF 1=0
BEGIN
CREATE TABLE ##UserInfo (
[UserId] int NOT NULL IDENTITY (1, 1),
[strEmail] varchar(50) NULL,
[strLastName] varchar(50) NULL,
[strFirstName] varchar(50) NULL,
);
END
It would be nice if you could put into a comment, but alas intellisense ignores that.

Related

Error Creating Database Diagram in SSMS 2012

When I attempt to create a database diagram, I get the following error:
Cannot insert the value NULL into column 'diagram_id', table 'MyDB.dbo.sysdiagrams'; column does
not allow nulls. INSERT fails.
The statement has been terminated.
The 'sp_creatediagram' procedure attempted to return a status of NULL, which is not allowed. A status of
0 will be returned instead. (.Net SqlClient Data Provider)
I am using SSMS 2012.
The database is set at a compatibility level of SQL Server 2012 (110)
##Version is Microsoft SQL Server 2012 - 11.0.5343.0 (X64)
Your problem is the diagram_ID when the table was created probably looked something like this
CREATE TABLE <table_name>
( diagram_ID INT NOT NULL PRIMARY KEY,
n...,
)
This basically means that a NULL value cannot be inserted into that column because of the NOT NULL condition. So an insert statement like:
INSERT INTO <table_name>
(Diagram_ID, n...,)
VALUES
(NULL, n...,)
Would fail because of the NULL you would need to have a value in there like (since I called it an integer):
INSERT INTO <table_name>
(Diagram_ID, n...,)
VALUES
(23, n...,)
The column may also be an indentity column in which case you have no controll over what can be inserted into the table.
Go to system tables and look for systemdiagrams table, and turn to YES the "indentity Specification" property for the field diagram_id
Hope this will help you, this script solved my issues
DROP TABLE dbo.sysdiagrams;
GO
CREATE TABLE [dbo].[sysdiagrams]
(
[name] [sysname] NOT NULL,
[principal_id] [int] NOT NULL,
[diagram_id] [int] IDENTITY(1,1) PRIMARY KEY,
[version] [int] NULL,
[definition] [varbinary](max) NULL,
CONSTRAINT [UK_principal_name] UNIQUE ([principal_id],[name])
);
GO
EXEC sys.sp_addextendedproperty
#name=N'microsoft_database_tools_support',
#value=1 ,
#level0type=N'SCHEMA',
#level0name=N'dbo',
#level1type=N'TABLE',
#level1name=N'sysdiagrams';
GO

Drop temp table if it exists

Friends,
I am creating a temp table. The script may be run several times so I need to check if the temp table exist then drop it. I have the written the code below but I get an error when running the script twice, that the table already exists:
There is already an object named '#lu_sensor_name_19' in the database.
It appears that IF OBJECT_ID('alarm..#lu_sensor_name_19') IS NOT NULL does not return true when the tablle is not null. What am I doing wrong?
IF OBJECT_ID('alarm..#lu_sensor_name_19') IS NOT NULL
BEGIN
DROP TABLE #lu_sensor_name_19
END
CREATE TABLE #lu_sensor_name_19(
sensorname_id int NOT NULL,
sensorname nvarchar(50) NOT NULL,
paneltype_id smallint NOT NULL,
panel_version_id int NULL,
prefix_allowed tinyint NOT NULL,
base_allowed tinyint NOT NULL,
suffix_allowed tinyint NOT NULL,
key_value int NULL,
sort_index int NULL,
device_allowed tinyint NOT NULL,
sensor_name_group_id smallint NOT NULL,
)
Temp #Tables are created in tempdb. Try this:
IF OBJECT_ID('tempdb..#lu_sensor_name_19') IS NOT NULL
BEGIN
DROP TABLE #lu_sensor_name_19
END
CREATE TABLE #lu_sensor_name_19...
SQL Server 2016 added the ability to do the drop in one line:
DROP TABLE IF EXISTS #lu_sensor_name_19
CREATE TABLE #lu_sensor_name_19...
Use this.
IF OBJECT_ID('tempdb.dbo.##myTempTable', 'U') IS NOT NULL
BEGIN
DROP TABLE ##myTempTable;
--DROP TABLE ##tempdb.dbo.myTempTable;
/* Above line commented out, because it generates warning:
"Database name 'tempdb' ignored, referencing object in tempdb.",
which is a pain in the neck if you are using a temp table to generate SQL code,
and want to print the code to the screen.*/
END;
GO
CREATE TABLE ##myTempTable(
FooBar nvarchar(128) not null,
);
And, in SQL Server 2016, you can write:
DROP TABLE IF EXISTS ##myTempTable

Creating table with T-SQL - can't see created tables in Object explorer

use Microsoft SQL
Try to create table using T-SQL language. Code is very simple, executed succesfull, but i don't see created table in Object Explorer. Try restart/reconnect/refresh/reexecuting - result the same - cant see this table.
Also try to do it manually (by right click mouse in tree in the Object explorer - all ok - can see just created table).
Code:
USE tempdb
IF OBJECT_ID('dbo.Employees','U') IS NOT NULL
DROP TABLE dbo.Employees;
CREATE TABLE dbo.Employees
(
empid INT NOT NULL,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
hiredate DATE NOT NULL,
mgrid INT NULL,
ssn VARCHAR(20) NOT NULL,
salary MONEY NOT NULL
);
Screen shot
Think that the problem is very simple, but try to find answer and stack a little bit. Why i don't see just created table? Is this code is correct for creating table?
It is because you are creating that table in TempDB.
From this drop down select the right DataBase.
OR
execute the following statement before you execute the Create Table Statement. Something like
USE TestDB
GO
Create Table ....

SQL Server trigger update column from another table

I am trying to create a fairly simple SQL Server trigger, hope someone can help.
I have a table with structure like this:
Table #1:
CREATE TABLE `teg_priority` (
`UCIDN` BIGINT(50) NULL DEFAULT NULL,
`CIDN` BIGINT(50) NOT NULL,
`CustomerName` VARCHAR(200) NOT NULL,
`NGM` VARCHAR(150) NULL DEFAULT NULL,
`Service_Manager` VARCHAR(150) NULL DEFAULT NULL,
`CBS` LONGTEXT NULL,
`Tag` VARCHAR(50) NOT NULL,
PRIMARY KEY (`CIDN`)
)
and another table (table #2):
CREATE TABLE `custalign` (
`UCIDN` BIGINT(20) NOT NULL,
`CIDN` BIGINT(20) NOT NULL,
`CustomerName` VARCHAR(255) NOT NULL,
PRIMARY KEY (`CIDN`)
)
I am trying to set up a trigger where every time a new record is inserted into the first table that the following query will be run as a trigger to update field UCIDN in table 1
update teg_priority
set teg_priority.UCIDN = (select UCIDN from custalign
where teg_priority.CIDN = custalign.CIDN)
The above query works i just don't know how to write it as a trigger statement.
Please help.
CREATE TRIGGER dbo.Teg_priority_after_insert
ON dbo.teg_priority AFTER INSERT
AS
UPDATE inserted
set inserted.UCIDN = (select UCIDN from custalign
where inserted.CIDN = custalign.CIDN)
That's your answer. You might consider a change in approach; assuming it doesn't require a total re-work of your process-flow. I can't really suggest more without knowing what you're ultimately trying to accomplish.
In SQL Server triggers, there is an inserted and a deleted table automatically-generated to which you may refer. Each respectively contains the new and old records as a result of whatever statement AFTER [INSERT],[UPDATE],[DELETE]. The inserted table is accessible to AFTER INSERT and UPDATE triggers, while the deleted table is accessible to AFTER UPDATE and DELETE triggers.
That might be more than you wanted to know, but I thought you'd benefit from a brief explanation of where the inserted table came from in my code.
[Insert all the usual caveats about trying not to use triggers wherever possible here.]
try out this..hope this will helps you
For MySQL
CREATE TRIGGER teg_priorityTrigger AFTER INSERT ON teg_priority
FOR EACH ROW
BEGIN
UPDATE inserted
set inserted.UCIDN = (select UCIDN from custalign
where inserted.CIDN = custalign.CIDN)
END
For SQL Server
CREATE TRIGGER teg_priorityTrigger ON dbo.teg_priority AFTER INSERT
AS
UPDATE inserted
set inserted.UCIDN = (select UCIDN from custalign
where inserted.CIDN = custalign.CIDN)
hope this will helps you...

Altering SQL table to add column

I currently have a table with four columns - i wanted to add a fifth column but having some trouble.
I open the table in sql server studio management 2008 and i added the column info like so:
CREATE TABLE [dbo].[Case]
(
CaseId UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
CaseNumber NVARCHAR(50) NOT NULL,
CourtId INT NOT NULL,
DateOpened DATETIME NOT NULL,
)
my addition:
CREATE TABLE [dbo].[Case]
(
CaseId UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
CaseNumber NVARCHAR(50) NOT NULL,
CaseName NVARCHAR(50),
CourtId INT NOT NULL,
DateOpened DATETIME NOT NULL,
)
After adding CaseName column, i tried executing the table in Management Studio but i got the error message "There is already an object named 'Case' in the database."
I tried saving and then building my database hoping that the column will be added but that wasn't successful. I tried a New Query and writing the 'Alter table "case" add CaseName nvarchar(50) but again without luck. It shows that the file is changed with the new column because i saved it but after building my overall database it isn't making any changes. Any helpful tips will be great.
You want to ALTER, as follows:
ALTER TABLE [dbo].[Case] ADD CaseName NVARCHAR(50)
Better yet, you can check for the existance of the column first:
if not exists (SELECT 1 FROM sysobjects INNER JOIN syscolumns ON
sysobjects.id = syscolumns.id
WHERE sysobjects.name = N'Case' AND syscolumns.name = N'CaseName')
ALTER TABLE [dbo].[Case] ADD CaseName NVARCHAR(50)
you should try this
ALTER TABLE [dbo].[Case]
ADD CaseName NVARCHAR(50)
You are trying to create another table Case but one already exists that's why you have an error. When you want to edit a table, you have to use Alter table
Use an Alter table statement instead of Create
If you can't get the Alter statement to work for some reason, you could also drop the existing table and create a new one with the new field, but all your existing rows will be lost.
If you're using SSMS, you can Design the table instead of Edit to add the column.
ALTER is what you need to investigate (F1)
An alternative is.
Create a new table
CREATE TABLE [dbo].[Case2]
(
CaseId UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
CaseNumber NVARCHAR(50) NOT NULL,
CourtId INT NOT NULL,
DateOpened DATETIME NOT NULL,
newcolumn INT NULL
)
Move data from existing table into the new one
INSERT INTO [dbo].[Case2]
SELECT * FROM [dbo].[Case]
Then
DROP TABLE [dbo].[Case]
Then in management studio right-click 'Case2' and re-name it 'Case'
I recommend checking for the existence of the column prior to adding it, especially important when you work with migration scripts.
Here is how I usually do it:
IF NOT EXISTS(SELECT * FROM sys.columns WHERE Name = N'ColumnName' AND Object_ID = Object_ID(N'TableName'))
BEGIN
ALTER TABLE [dbo].TableName ADD ColumnName NVARCHAR(512) null
END