How do I use an SQLCMD variable to define a table name? - tsql

So, using SSDT on Visual Studio 2013, I can specify a Database name...
create view vSample1 as
select * from [$(RandomDatabase)].dbo.TableName
I can also do this with a four part name
create view vSample2 as
select * from [$(RandomServer)].[$(RandomDatabase)].dbo.TableName
But when I try to do this with the TableName, I get errors...
create view vSample3
select * from [$(RandomServer)].[$(RandomDatabase)].dbo.[$(RandomTable)]
It gives me an error similiar to
Error: SQL71561: View: [vSample3] has an unresolved reference to object [$(RandomServer)].[$(RandomDatabase)].dbo.[$(RandomTable)].
I've been looking at the SQLCMD variables window in the project settings and have verified that the $(RandomTable) variable is defined as TableName, but it still gives me build errors.
Why is this, and how do I fix it?
Thanks

I would use pre/post deployment script to create such view using sp_executesql and dynamic query.
exec sp_executesql N'create view vSample3 as
select * from [$(RandomServer)].[$(RandomDatabase)].dbo.[$(RandomTable)]'

I would use synonyms - create a table which matches the actual table and create a synonym that points to that, then reference the synonym in the view or just reference instead of the view. You need this so you get it to compile and you get the good stuff like references etc.
Then when you deploy to each server just deploy the correct synonym to the correct server.
(Then kick your vendor in the b&DD$ for such an annoying system)

Related

Object reference exception when importing content in Episerver

We are using Optimizely/Episerver CMS 11.20. When trying to export a page hierarchy from our production environment and then import the resulting ExportedFile.episerverdata file to our acceptance test environment I get the following error:
[Importing content 70725_133679] Exception: Object reference not set to an instance of an object.
I have no idea what 70725_133679 is referring to. Is there a way to look this up, e.g. via an SQL query against the "EPi" database?
It refers to a specific version of some content (which could be just about anything).
You could try to browse to https://yoursite/EPiServer/CMS/#context=epi.cms.contentdata:///70725_133679 (note the ID at the end) to see which content it is.
Got another answer on a Optimizely forum (thanks to Surjit Bharath):
The following SQL against the EPi database gives information about the referenced content:
select * from tblContent where pkID = 70725 
select * from tblWorkContent where pkID = 133679
This too points to a submit button. I have yet to understand why that block would cause an exception during import, but now I at least have a place to start digging.

Command Manager Create Fact Script not seeing table

I keep getting a message when I try to run the script below in command manager, that the table isn't in the folder. However when I look in the folder the table is right there, with "logical table" in the details column. Can anyone see what the issue is with my script?
Code:
CREATE FACT "TestFact" IN FOLDER "\Schema Objects\Facts\BulkFactTest" EXPRESSION "StuffCount" EXPSOURCETABLES "\SCHEMA OBJECTS\TABLES\TableName" FOR PROJECT "ProjectName";
Just checked and it seems you have to omit the base path to the table. Check the outline-example for "Create Fact" of the CommandManager (always helpful).
So this will work:
CREATE FACT "TestFact" IN FOLDER "\Schema Objects\Facts\BulkFactTest" EXPRESSION "StuffCount" EXPSOURCETABLES "TableName" FOR PROJECT "ProjectName";
Note: even when moving tables/table-aliases to subfolders you have to only provide the table-name in EXPSOURCETABLES. Sadly documentation is not the strong suit of MSTR.

See actual code of function in DB2 for IBM i

Hello guy I create a function in DB2 UDB for AS/400 version 07.01.0000 V7R1m0
I use a windows with dbvisualizer to connect the server.
My function is...
CREATE FUNCTION JVAOBJ.BNOWPAPOL(POL VARCHAR(10)) RETURNS DECIMAL(7,7) LANGUAGE SQL NOT
DETERMINISTIC READS SQL DATA
RETURN
(
SELECT
CASE
WHEN NUM IN (1,2)
THEN 0.3
ELSE 0.19698
END AS VALOR
FROM
LMDDTA.VERT240
WHERE
POLLIFE = POL )
It return 0.3 or 0.19698 depending of POL param
To do it I delete DROP FUNCTION JVAOBJ.BNOWPAPOL and run CREATE until work well.
My problem is I can not see the actual code of the function in dbvisualizer I cant see the function created
How I can see the actual code?
Note:
The server administrator has access to the console as400 (yes, that black screen with green letters or orange letters, which I have not much knowledge)
maybe, I can see it from here.
Note 2:
I use jt400 driver to connect.
Try IBM i Navigator for web. If it is configured on your machine, you can reach it through this URL: https://your.ibm.i:2005/ibm/console/login.do?action=secure
If it is not configured, then perhaps you can ask the admin to install the Windows client. It is part of Client Access for Windows and is called IBM i Navigator for Windows.
In either case, use the navigation tree to go to Databases > machine > Schemas > JVAOBJ > Functions. Right click your function > Definition and then choose Routine Body
EDIT Add SYSROUTINE
Another way to see the routine body is via the DB2 catalog table SYSROUTINE. select specific_schema, specific_name, routine_definition from sysroutine
I know IBM i Navigator its a great tool, but you need some amount of knowledge to master it.
The easiest way is to query the SQL system tables using dbvisualizer
SELECT * FROM qsys2/sysfuncs
WHERE (SPECIFIC_SCHEMA, SPECIFIC_NAME) = ('JVAOBJ', 'BNOWPAPOL')

Fully qualified name with dollar sign in SSDT project

I was working on a tsql project, and I have noticed that the existing code used a syntax that I have not seen before. They have put a dollar sign in front of the database name for fully qualified address.
Here is one example :
SELECT c.AccountCode, FROM **[$(SmartAdmin)]**.dbo.Customers c
If I rename the database name as SmartAdmin.dbo.Customers, Visual Studio throws error says "contains an unresolved reference to an object".
It appears to be a Visual Studio related thing, can anyone explain what is this and
whether I can remove it.
Please see the attached screenshots, the last one comes from project solution file.
The [$(SmartAdmin)] syntax is used in an SSDT project to reference objects in other databases. To be precise, it is SQLCMD syntax.
If you have a database reference to SmartAdmin, and if the reference is set up so that [$(SmartAdmin)] properly references it, then that is not the problem. It looks like the problem is the other two references to [SmartAdmin]. Change them to look like [$(SmartAdmin)].dbo.whatever.
Example of why these references are useful: I just edited a stored procedure I had in SSDT to misspell a column name. Within seconds, the column name was underlined in red. I then deleted the database reference the column name depended on. The red underline went away. I added the database reference back, and the column was once again underlined in red. I corrected the column name, and the red underline went away.
Without the database reference, I would have had to wait until the stored procedure was deployed, or possibly executed, to see the error. With the database reference, I found out about the problem in the editor. Just like code.
It appears to me that your database is actually called $(SmartAdmin)
As the error suggests "contains an unresolved reference to an object". which means when you change the database name to SmartAdmin, it cannot find a database with this name on server.
If you want to change the database name you will need to execute the following statement and then you will be able to use that name in your statements.
USE master;
GO
EXEC sp_renamedb '$(SmartAdmin)', 'SmartAdmin';
GO
USE SmartAdmin; --<-- Now you will be able to connect to this database
GO -- without the dollar ($) sign.
This issue relates to Database Project External Database References. When you add another solution to a database project, you can specify the "Database Variable Name", in my case, it is $(SmartAdmin).
Please see this link for more information.
Thanks everybody for answering my question.

perl create view is not working

I work with perl and sybase. I try to create view in perl:
if(condition)
create view name_view
as select col1,col2,col3
from table1,table2
where key1=key2
else (condition)
create view name_view
as select col1,col2,col2
from table1,table3
where key1=key2
And when I run it, I always get an error, that view is already exists.
Even I put an if - exists condition.
drop view name_view
it is always the same error.
Please note: I cannot create a view in separate query, because when I select from view, it works very slow.
I would recommend using prepare and execute call. Please read this excellent post from PerlMonks.org