Can you use "Drop table ......." in SSRS - sql

I am creating a report which drops the table if it exists at the beginning and inserts the data that is needed.
The SQl runs smoothly in SQL Server Management Studio with no issues but when I place it into SSRS to create a report, it is saying that the synax is wrong.
I think I might need to do this as a stored procedure but I wanted to make sure that I have not missed anything first.
Thanks in advance.

Place the code in a Stored procedure, and use temporary tables or table variables rather.
Have a look at
CREATE TABLE (Transact-SQL)
Temporary Tables
You can create local and global temporary tables.
Local temporary tables are visible only in the current session, and
global temporary tables are visible to all sessions. Temporary tables
cannot be partitioned.
Prefix local temporary table names with single number sign
(#table_name), and prefix global temporary table names with a double
number sign (##table_name).
SQL statements reference the temporary table by using the value
specified for table_name in the CREATE TABLE statement
Also have a look at
DECLARE #local_variable (Transact-SQL)
#table_variable_name
Is the name of a variable of type table. Variable names must begin
with an at (#) sign and conform to the rules for identifiers.
Defines the table data type. The table declaration includes column
definitions, names, data types, and constraints. The only constraint
types allowed are PRIMARY KEY, UNIQUE, NULL, and CHECK. An alias data
type cannot be used as a column scalar data type if a rule or default
definition is bound to the type.
is a subset of information used to define a
table in CREATE TABLE. Elements and essential definitions are included
here. For more information, see CREATE TABLE (Transact-SQL).

Related

If not exist clause SQL statement

so I found this sql query in a project I am succeeding. This is the first time I encountering this clause/statement. I understand that this is to look if the table exist before creating one and that Object_ID is the table name that is to be created.
My questions are:
Does sysobject mean the database?
What is the Object property?
I know that it is not the columns inside the table to be created.
The columns are : dtb_color_id and description.
can someone explain this to me. please?
IF NOT EXISTS(SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('DTB_COLOR') AND OBJECTPROPERTY(ID,'ISUserTable') = 1)
BEGIN
.......some query I understand
END
sysobjects, OBJECTPROPERTY and OBJECT_ID are used in Microsoft SQL Server. They are part of the SQL Server DMVs and system functions/procedures used to query and manipulate the metadata.
sys.sysobjects is simply the list of all objects (tables, views, SPs, functions, etc) on the server in the active database. Please note, that sys.sysobjects is deprecated and is only available for backward compatibility. Use sys.objects instead
https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/system-dynamic-management-views?view=sql-server-ver16
It has (as far as I know) no meaning in MySQL, unless somebody specifically created them.
You can also use INFORMATION_SCHEMA which is available in MySQL too (however slightly different in different RDBMS).
MSSQL INFORMATION_SCHEMA: https://learn.microsoft.com/en-us/sql/relational-databases/system-information-schema-views/system-information-schema-views-transact-sql?view=sql-server-ver16
MySQL INFORMATION_SCHEMA: https://dev.mysql.com/doc/refman/8.0/en/information-schema.html
SQL Server has no CREATE TABLE IF NOT EXISTS construct, a variation of the mentioned condition is commonly used to imitate that.
This is a way in SQL Server to check if a table exists in the active database and to perform actions according to the result, like creating the table.
OBJECTPROPERTY simply checks (in this case) if the table is a user created one.
https://learn.microsoft.com/en-us/sql/t-sql/functions/objectproperty-transact-sql?view=sql-server-ver16
I would remove the OBJECTPROPERTY condition in case the part you understand is a CREATE TABLE statement. You don't want to create a table which has a similar name to any system table/view, also you don't want to execute the CREATE TABLE if there is a VIEW with the same name (table creation will fail)
Yes sysobject means database.
The OBJECTPROPERTY() function returns information about schema-
scoped objects in the current database. Use this to check if an
object is a table, view, stored procedure, etc. You can also use
it to check if a table has a primary key, foreign key, foreign
key reference, etc.
For more details : https://learn.microsoft.com/en-us/sql/t-sql/functions/objectpropertyex-transact-sql?view=sql-server-ver16
In this scenario it is used to check whether it is user table or
not. The result of the ISUserTable property is 1 when it is user
table otherwise returns 0.
Here the following steps are followed:
First, it executes the select statement inside the IF Exists
If the select statement returns a value that condition is TRUE for IF Exists
It starts the code inside a begin statement
DTB_COLOR - May be a stored procedure

Generating SQL server script can get Create and alter statements for the same table

HI I am generating schema script for a database, but when i finish creating it and and look at the script it gives create table statement for a table but not including all column in it also it generates alter table add column statement for the same tables but for missing columns which are left in create table statement.
see the attached screenshot.
Assuming the question is "why does it not create all of the columns in the CREATE TABLE statement" ...
You'll notice that between the CREATE TABLE and the ALTER statements, the value for SET ANSI_PADDING is altered. As the documentation notes, the setting's value is taken into account at the point in time when a column is created.
There's no way to override this setting in-line with the declaration of a column.
Since your table apparently contains a mixture of columns, some of which are defined with the setting ON and others with it defined OFF, there's no way to write a single CREATE TABLE statement that creates all of the columns in one go.

Hana: How to create a table type LIKE the type of another table?

I'm trying to create a table type which has a lot of fields, in SQLScript for a Hana machine.
I've tried some combinations of 'Like' and other keywords but it all comes out as a syntax error.
Furthermore, I could not find any hint of this in the SQLScript reference guide.
I've been creating tables LIKE [orignal table] with no data and inserting records into it - not practical :(
Thanks in advance.
Miguel
EDIT: to understand if the procedure 'get_object_definition' can be used with tables with case-sensitive names.
In this image we can see the procedure calls, with the error message; in the image after, the tables and table types in each of the schemas.
EDITED: I got it, have to call the procedure with ' " table_name " '
There is no specific command to create a type based on an existing table or another type.
What you can do is to get the definition of the table via
call get_object_definition ('<schema name>', '<table name>');
and edit the object creation statement to a CREATE TYPE statement. This is basically just changing the starting part of the statement and cutting away some parts at the end.

Where is #Temp Stored ? OR How is #temp Stored

I know #temp is temp. Table valid for particular session only. but if i define #temp in two different session, and run them simultaneously will that conflict. if no then how actually these tables are stored in memory. And how is that different from ##Temp?????
Temporary tables with a single # are "local", while those with a double ## are "global".
Local ones will drop out of scope once the stored procedure that defines them is done.
Global ones can be used by other users, or by the same user from different stored procedures, or by multiple calls of the same procedure. They will get dropped only after the last user that was referencing them is no longer referencing them, i.e. after the last stored proc is complete.
All are stored in the tempdb database; none in the "memory".
From CREATE TABLE
The full name of a temporary table as stored in the sysobjects table
in tempdb is made up of the table name specified in the CREATE TABLE
statement and the system-generated numeric suffix.
So it is stored in the tempdb.
Also from Temporary Tables in SQL Server
Temporary tables and table variables are created in the TempDB
database

How to reference CLR UDTs when creating a table

I have no idea why there is very little documentation on this so I'll ask here.
I have created some user defined data types and would like to use them when creating a table.
However I have no idea what the syntax is for calling to them.
Yes I know it's best to STAY AWAY from them but I'm in a position where I am forced to work with them.
You use User Defined Types (UDTs) just like the built-in types.
Defining UDT Tables and Columns
There is no special syntax for creating a UDT column in a table. You
can use the name of the UDT in a column definition as though it were
one of the intrinsic SQL Server data types.
The following CREATE TABLE Transact-SQL statement creates a table
named Points, with a column named ID, which is defined as an int
identity column and \ the primary key for the table. The second column
is named PointValue, with a data type of Point. The schema name used
in this example is dbo. Note that you must have the necessary
permissions to specify a schema name. If you omit the schema name, the
default schema for the database user is used.
CREATE TABLE dbo.Points
(
ID int IDENTITY(1,1) PRIMARY KEY,
PointValue Point
)
Registering User-Defined Types in SQL Server